Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Add option to explicitly specify waitTimeout #212

Merged
merged 1 commit into from
Aug 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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