page.waitFor should send ...args to page.waitForFunction (#775)

This lets the user pass `...args` into `page.waitFor`. It also clarifies that the docs that `options` is not optional if `...args` are specified.

Fixes #770
This commit is contained in:
JoelEinbinder 2017-09-15 14:28:15 -07:00 committed by Andrey Lushnikov
parent d562db3140
commit e2cad568d6
4 changed files with 21 additions and 14 deletions

View File

@ -74,8 +74,8 @@
+ [page.type(text, options)](#pagetypetext-options) + [page.type(text, options)](#pagetypetext-options)
+ [page.url()](#pageurl) + [page.url()](#pageurl)
+ [page.viewport()](#pageviewport) + [page.viewport()](#pageviewport)
+ [page.waitFor(selectorOrFunctionOrTimeout[, options])](#pagewaitforselectororfunctionortimeout-options) + [page.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#pagewaitforselectororfunctionortimeout-options-args)
+ [page.waitForFunction(pageFunction[, options, ...args])](#pagewaitforfunctionpagefunction-options-args) + [page.waitForFunction(pageFunction[, options[, ...args]])](#pagewaitforfunctionpagefunction-options-args)
+ [page.waitForNavigation(options)](#pagewaitfornavigationoptions) + [page.waitForNavigation(options)](#pagewaitfornavigationoptions)
+ [page.waitForSelector(selector[, options])](#pagewaitforselectorselector-options) + [page.waitForSelector(selector[, options])](#pagewaitforselectorselector-options)
* [class: Keyboard](#class-keyboard) * [class: Keyboard](#class-keyboard)
@ -111,8 +111,8 @@
+ [frame.parentFrame()](#frameparentframe) + [frame.parentFrame()](#frameparentframe)
+ [frame.title()](#frametitle) + [frame.title()](#frametitle)
+ [frame.url()](#frameurl) + [frame.url()](#frameurl)
+ [frame.waitFor(selectorOrFunctionOrTimeout[, options])](#framewaitforselectororfunctionortimeout-options) + [frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#framewaitforselectororfunctionortimeout-options-args)
+ [frame.waitForFunction(pageFunction[, options, ...args])](#framewaitforfunctionpagefunction-options-args) + [frame.waitForFunction(pageFunction[, options[, ...args]])](#framewaitforfunctionpagefunction-options-args)
+ [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options) + [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options)
* [class: ElementHandle](#class-elementhandle) * [class: ElementHandle](#class-elementhandle)
+ [elementHandle.click([options])](#elementhandleclickoptions) + [elementHandle.click([options])](#elementhandleclickoptions)
@ -844,9 +844,10 @@ This is a shortcut for [page.mainFrame().url()](#frameurl)
- `hasTouch`<[boolean]> Specifies if viewport supports touch events. Defaults to `false` - `hasTouch`<[boolean]> Specifies if viewport supports touch events. Defaults to `false`
- `isLandscape` <[boolean]> Specifies if viewport is in landscape mode. Defaults to `false`. - `isLandscape` <[boolean]> Specifies if viewport is in landscape mode. Defaults to `false`.
#### page.waitFor(selectorOrFunctionOrTimeout[, options]) #### page.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])
- `selectorOrFunctionOrTimeout` <[string]|[number]|[function]> A [selector], predicate or timeout to wait for - `selectorOrFunctionOrTimeout` <[string]|[number]|[function]> A [selector], predicate or timeout to wait for
- `options` <[Object]> Optional waiting parameters - `options` <[Object]> Optional waiting parameters
- `...args` <...[Serializable]> Arguments to pass to `pageFunction`
- returns: <[Promise]> - returns: <[Promise]>
This method behaves differently with respect to the type of the first parameter: This method behaves differently with respect to the type of the first parameter:
@ -855,9 +856,9 @@ This method behaves differently with respect to the type of the first parameter:
- if `selectorOrFunctionOrTimeout` is a `number`, than the first argument is treated as a timeout in milliseconds and the method returns a promise which resolves after the timeout - if `selectorOrFunctionOrTimeout` is a `number`, than the first argument is treated as a timeout in milliseconds and the method returns a promise which resolves after the timeout
- otherwise, an exception is thrown - otherwise, an exception is thrown
Shortcut for [page.mainFrame().waitFor(selectorOrFunctionOrTimeout[, options])](#framewaitforselectororfunctionortimeout-options). Shortcut for [page.mainFrame().waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#framewaitforselectororfunctionortimeout-options-args).
#### page.waitForFunction(pageFunction[, options, ...args]) #### page.waitForFunction(pageFunction[, options[, ...args]])
- `pageFunction` <[function]|[string]> Function to be evaluated in browser context - `pageFunction` <[function]|[string]> Function to be evaluated in browser context
- `options` <[Object]> Optional waiting parameters - `options` <[Object]> Optional waiting parameters
- `polling` <[string]|[number]> An interval at which the `pageFunction` is executed, defaults to `raf`. If `polling` is a number, then it is treated as an interval in milliseconds at which the function would be executed. If `polling` is a string, then it could be one of the following values: - `polling` <[string]|[number]> An interval at which the `pageFunction` is executed, defaults to `raf`. If `polling` is a number, then it is treated as an interval in milliseconds at which the function would be executed. If `polling` is a string, then it could be one of the following values:
@ -879,7 +880,7 @@ puppeteer.launch().then(async browser => {
browser.close(); browser.close();
}); });
``` ```
Shortcut for [page.mainFrame().waitForFunction(pageFunction[, options, ...args])](#framewaitforfunctionpagefunction-options-args). Shortcut for [page.mainFrame().waitForFunction(pageFunction[, options[, ...args]])](#framewaitforfunctionpagefunction-options-args).
#### page.waitForNavigation(options) #### page.waitForNavigation(options)
- `options` <[Object]> Navigation parameters which might have the following properties: - `options` <[Object]> Navigation parameters which might have the following properties:
@ -1196,9 +1197,10 @@ If the name is empty, returns the id attribute instead.
Returns frame's url. Returns frame's url.
#### frame.waitFor(selectorOrFunctionOrTimeout[, options]) #### frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])
- `selectorOrFunctionOrTimeout` <[string]|[number]|[function]> A [selector], predicate or timeout to wait for - `selectorOrFunctionOrTimeout` <[string]|[number]|[function]> A [selector], predicate or timeout to wait for
- `options` <[Object]> Optional waiting parameters - `options` <[Object]> Optional waiting parameters
- `...args` <...[Serializable]> Arguments to pass to `pageFunction`
- returns: <[Promise]> - returns: <[Promise]>
This method behaves differently with respect to the type of the first parameter: This method behaves differently with respect to the type of the first parameter:
@ -1208,7 +1210,7 @@ This method behaves differently with respect to the type of the first parameter:
- otherwise, an exception is thrown - otherwise, an exception is thrown
#### frame.waitForFunction(pageFunction[, options, ...args]) #### frame.waitForFunction(pageFunction[, options[, ...args]])
- `pageFunction` <[function]|[string]> Function to be evaluated in browser context - `pageFunction` <[function]|[string]> Function to be evaluated in browser context
- `options` <[Object]> Optional waiting parameters - `options` <[Object]> Optional waiting parameters
- `polling` <[string]|[number]> An interval at which the `pageFunction` is executed, defaults to `raf`. If `polling` is a number, then it is treated as an interval in milliseconds at which the function would be executed. If `polling` is a string, then it could be one of the following values: - `polling` <[string]|[number]> An interval at which the `pageFunction` is executed, defaults to `raf`. If `polling` is a number, then it is treated as an interval in milliseconds at which the function would be executed. If `polling` is a string, then it could be one of the following values:

View File

@ -361,15 +361,16 @@ class Frame {
/** /**
* @param {(string|number|function())} selectorOrTimeout * @param {(string|number|function())} selectorOrTimeout
* @param {!Object=} options * @param {!Object=} options
* @param {!Array<*>} args
* @return {!Promise} * @return {!Promise}
*/ */
waitFor(selectorOrFunctionOrTimeout, options = {}) { waitFor(selectorOrFunctionOrTimeout, options = {}, ...args) {
if (helper.isString(selectorOrFunctionOrTimeout)) if (helper.isString(selectorOrFunctionOrTimeout))
return this.waitForSelector(selectorOrFunctionOrTimeout, options); return this.waitForSelector(selectorOrFunctionOrTimeout, options);
if (helper.isNumber(selectorOrFunctionOrTimeout)) if (helper.isNumber(selectorOrFunctionOrTimeout))
return new Promise(fulfill => setTimeout(fulfill, selectorOrFunctionOrTimeout)); return new Promise(fulfill => setTimeout(fulfill, selectorOrFunctionOrTimeout));
if (typeof selectorOrFunctionOrTimeout === 'function') if (typeof selectorOrFunctionOrTimeout === 'function')
return this.waitForFunction(selectorOrFunctionOrTimeout, options); return this.waitForFunction(selectorOrFunctionOrTimeout, options, ...args);
return Promise.reject(new Error('Unsupported target type: ' + (typeof selectorOrFunctionOrTimeout))); return Promise.reject(new Error('Unsupported target type: ' + (typeof selectorOrFunctionOrTimeout)));
} }

View File

@ -724,10 +724,11 @@ class Page extends EventEmitter {
/** /**
* @param {(string|number|function())} selectorOrTimeout * @param {(string|number|function())} selectorOrTimeout
* @param {!Object=} options * @param {!Object=} options
* @param {!Array<*>} args
* @return {!Promise} * @return {!Promise}
*/ */
waitFor(selectorOrFunctionOrTimeout, options = {}) { waitFor(selectorOrFunctionOrTimeout, options = {}, ...args) {
return this.mainFrame().waitFor(selectorOrFunctionOrTimeout, options); return this.mainFrame().waitFor(selectorOrFunctionOrTimeout, options, ...args);
} }
/** /**

View File

@ -530,6 +530,9 @@ describe('Page', function() {
await page.waitFor({foo: 'bar'}).catch(e => error = e); await page.waitFor({foo: 'bar'}).catch(e => error = e);
expect(error.message).toContain('Unsupported target type'); expect(error.message).toContain('Unsupported target type');
})); }));
it('should wait for predicate with arguments', SX(async function() {
await page.waitFor((arg1, arg2) => arg1 !== arg2, {}, 1, 2);
}));
}); });
describe('Page.Events.Console', function() { describe('Page.Events.Console', function() {