Skip to content

Commit

Permalink
Add option to explicitly specify waitTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
janza committed Aug 9, 2017
1 parent eaf2465 commit 7c3888a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Chromeless provides TypeScript typings.
- [`setUserAgent(useragent: string)`](#api-setuseragent)
- [`click(selector: string)`](#api-click)
- [`wait(timeout: number)`](#api-wait-timeout)
- [`wait(selector: string)`](#api-wait-selector)
- [`wait(selector: string, timeout?: number)`](#api-wait-selector)
- [`wait(fn: (...args: any[]) => boolean, ...args: any[])`] - Not implemented yet
- [`focus(selector: string)`](#api-focus)
- [`press(keyCode: number, count?: number, modifiers?: any)`](#api-press)
Expand Down Expand Up @@ -126,17 +126,19 @@ await chromeless.wait(1000)

<a name="api-wait-selector" />

### wait(selector: string): Chromeless<T>
### wait(selector: string, timeout?: number): Chromeless<T>

Wait until something appears. Useful for waiting for things to render.

__Arguments__
- `selector` - DOM selector to wait for
- `timeout` - How long to wait for element to appear (default is value of waitTimeout option)

__Example__

```js
await chromeless.wait('div#loaded')
await chromeless.wait('div#loaded', 1000)
```

---------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class Chromeless<T extends any> implements Promise<T> {
}

wait(timeout: number): Chromeless<T>
wait(selector: string): Chromeless<T>
wait(selector: string, timeout?: number): Chromeless<T>
wait(fn: (...args: any[]) => boolean, ...args: any[]): Chromeless<T>
wait(firstArg, ...args: any[]): Chromeless<T> {
switch (typeof firstArg) {
Expand All @@ -92,7 +92,7 @@ export default class Chromeless<T extends any> implements Promise<T> {
break
}
case 'string': {
this.queue.enqueue({ type: 'wait', selector: firstArg })
this.queue.enqueue({ type: 'wait', selector: firstArg, timeout: args[0] })
break
}
case 'function': {
Expand Down
15 changes: 9 additions & 6 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export default class LocalRuntime {
case 'goto':
return this.goto(command.url)
case 'wait': {
if (command.timeout) {
if (command.selector) {
return this.waitSelector(command.selector, command.timeout)
} else if (command.timeout) {
return this.waitTimeout(command.timeout)
} else if (command.selector) {
return this.waitSelector(command.selector)
} else {
throw new Error('waitFn not yet implemented')
}
Expand Down Expand Up @@ -122,9 +122,12 @@ export default class LocalRuntime {
await wait(timeout)
}

private async waitSelector(selector: string): Promise<void> {
this.log(`Waiting for ${selector}`)
await waitForNode(this.client, selector, this.chromelessOptions.waitTimeout)
private async waitSelector(
selector: string,
waitTimeout: number = this.chromelessOptions.waitTimeout
): Promise<void> {
this.log(`Waiting for ${selector} ${waitTimeout}`)
await waitForNode(this.client, selector, waitTimeout)
this.log(`Waited for ${selector}`)
}

Expand Down

0 comments on commit 7c3888a

Please sign in to comment.