Skip to content

Commit

Permalink
Merge pull request schickling#212 from janza/explicit-wait
Browse files Browse the repository at this point in the history
Add option to explicitly specify waitTimeout
  • Loading branch information
adieuadieu authored and webpolis committed Oct 5, 2017
2 parents d47e016 + 7c3888a commit fb44dd4
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
- [`clearCache()`](docs/api.md#api-clearcache)
- [`focus(selector: string)`](#api-focus)
Expand Down Expand Up @@ -127,17 +127,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 @@ -91,7 +91,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 @@ -100,7 +100,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 @@ -57,10 +57,10 @@ export default class LocalRuntime {
case 'setViewport':
return setViewport(this.client, command.options)
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 @@ -149,9 +149,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 fb44dd4

Please sign in to comment.