Skip to content

Commit de0a2d1

Browse files
authored
api(waitForLoadState): move waitUntil to be a first parameter (#1490)
1 parent 45a175d commit de0a2d1

8 files changed

+64
-56
lines changed

docs/api.md

+37-29
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ const [page] = await Promise.all([
325325
console.log(await page.evaluate('location.href'));
326326
```
327327

328-
> **NOTE** Use [Page.waitForLoadState](#pagewaitforloadstateoptions) to wait until the page gets to a particular state (you should not need it in most cases).
328+
> **NOTE** Use [`page.waitForLoadState([state[, options]])`](#pagewaitforloadstatestate-options) to wait until the page gets to a particular state (you should not need it in most cases).
329329
330330
#### browserContext.addCookies(cookies)
331331
- `cookies` <[Array]<[Object]>>
@@ -692,7 +692,7 @@ page.removeListener('request', logRequest);
692692
- [page.waitFor(selectorOrFunctionOrTimeout[, options[, arg]])](#pagewaitforselectororfunctionortimeout-options-arg)
693693
- [page.waitForEvent(event[, optionsOrPredicate])](#pagewaitforeventevent-optionsorpredicate)
694694
- [page.waitForFunction(pageFunction, arg[, options])](#pagewaitforfunctionpagefunction-arg-options)
695-
- [page.waitForLoadState([options])](#pagewaitforloadstateoptions)
695+
- [page.waitForLoadState([state[, options]])](#pagewaitforloadstatestate-options)
696696
- [page.waitForNavigation([options])](#pagewaitfornavigationoptions)
697697
- [page.waitForRequest(urlOrPredicate[, options])](#pagewaitforrequesturlorpredicate-options)
698698
- [page.waitForResponse(urlOrPredicate[, options])](#pagewaitforresponseurlorpredicate-options)
@@ -780,7 +780,8 @@ const [popup] = await Promise.all([
780780
]);
781781
console.log(await popup.evaluate('location.href'));
782782
```
783-
> **NOTE** Use [Page.waitForLoadState](#pagewaitforloadstateoptions) to wait until the page gets to a particular state (you should not need it in most cases).
783+
784+
> **NOTE** Use [`page.waitForLoadState([state[, options]])`](#pagewaitforloadstatestate-options) to wait until the page gets to a particular state (you should not need it in most cases).
784785
785786
#### event: 'request'
786787
- <[Request]>
@@ -1705,25 +1706,33 @@ await page.waitForFunction(selector => !!document.querySelector(selector), selec
17051706

17061707
Shortcut for [page.mainFrame().waitForFunction(pageFunction, arg, options]])](#framewaitforfunctionpagefunction-arg-options).
17071708

1708-
#### page.waitForLoadState([options])
1709-
- `options` <[Object]> Navigation parameters which might have the following properties:
1710-
- `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [browserContext.setDefaultNavigationTimeout(timeout)](#browsercontextsetdefaultnavigationtimeouttimeout), [browserContext.setDefaultTimeout(timeout)](#browsercontextsetdefaulttimeouttimeout), [page.setDefaultNavigationTimeout(timeout)](#pagesetdefaultnavigationtimeouttimeout) or [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) methods.
1711-
- `waitUntil` <"load"|"domcontentloaded"|"networkidle0"|"networkidle2"> When to consider navigation succeeded, defaults to `load`. Events can be either:
1712-
- `'load'` - consider navigation to be finished when the `load` event is fired.
1713-
- `'domcontentloaded'` - consider navigation to be finished when the `DOMContentLoaded` event is fired.
1714-
- `'networkidle0'` - consider navigation to be finished when there are no more than 0 network connections for at least `500` ms.
1715-
- `'networkidle2'` - consider navigation to be finished when there are no more than 2 network connections for at least `500` ms.
1716-
- returns: <[Promise]> Promise which resolves when the load state has been achieved.
1709+
#### page.waitForLoadState([state[, options]])
1710+
- `state` <"load"|"domcontentloaded"|"networkidle0"|"networkidle2"> Load state to wait for, defaults to `load`. If the state has been already reached while loading current document, the method resolves immediately.
1711+
- `'load'` - wait for the `load` event to be fired.
1712+
- `'domcontentloaded'` - wait for the `DOMContentLoaded` event to be fired.
1713+
- `'networkidle0'` - wait until there are no more than 0 network connections for at least `500` ms.
1714+
- `'networkidle2'` - wait until there are no more than 2 network connections for at least `500` ms.
1715+
- `options` <[Object]>
1716+
- `timeout` <[number]> Maximum waiting time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [browserContext.setDefaultNavigationTimeout(timeout)](#browsercontextsetdefaultnavigationtimeouttimeout), [browserContext.setDefaultTimeout(timeout)](#browsercontextsetdefaulttimeouttimeout), [page.setDefaultNavigationTimeout(timeout)](#pagesetdefaultnavigationtimeouttimeout) or [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) methods.
1717+
- returns: <[Promise]> Promise which resolves when the required load state has been reached.
17171718

1718-
This resolves when the page reaches a required load state, `load` by default. The navigation can be in progress when it is called.
1719-
If navigation is already at a required state, resolves immediately.
1719+
This resolves when the page reaches a required load state, `load` by default. The navigation must have been committed when this method is called. If current document has already reached the required state, resolves immediately.
17201720

17211721
```js
17221722
await page.click('button'); // Click triggers navigation.
1723-
await page.waitForLoadState(); // The promise resolves after navigation has finished.
1723+
await page.waitForLoadState(); // The promise resolves after 'load' event.
1724+
```
1725+
1726+
```js
1727+
const [popup] = await Promise.all([
1728+
page.waitForEvent('popup'),
1729+
page.click('button'), // Click triggers a popup.
1730+
])
1731+
await popup.waitForLoadState('domcontentloaded'); // The promise resolves after 'domcontentloaded' event.
1732+
console.log(await popup.title()); // Popup is ready to use.
17241733
```
17251734

1726-
Shortcut for [page.mainFrame().waitForLoadState([options])](#framewaitforloadstateoptions).
1735+
Shortcut for [page.mainFrame().waitForLoadState([options])](#framewaitforloadstatestate-options).
17271736

17281737
#### page.waitForNavigation([options])
17291738
- `options` <[Object]> Navigation parameters which might have the following properties:
@@ -1881,7 +1890,7 @@ An example of getting text from an iframe element:
18811890
- [frame.url()](#frameurl)
18821891
- [frame.waitFor(selectorOrFunctionOrTimeout[, options[, arg]])](#framewaitforselectororfunctionortimeout-options-arg)
18831892
- [frame.waitForFunction(pageFunction, arg[, options])](#framewaitforfunctionpagefunction-arg-options)
1884-
- [frame.waitForLoadState([options])](#framewaitforloadstateoptions)
1893+
- [frame.waitForLoadState([state[, options]])](#framewaitforloadstatestate-options)
18851894
- [frame.waitForNavigation([options])](#framewaitfornavigationoptions)
18861895
- [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options)
18871896
<!-- GEN:stop -->
@@ -2369,22 +2378,21 @@ const selector = '.foo';
23692378
await frame.waitForFunction(selector => !!document.querySelector(selector), selector);
23702379
```
23712380

2372-
#### frame.waitForLoadState([options])
2373-
- `options` <[Object]> Navigation parameters which might have the following properties:
2374-
- `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [browserContext.setDefaultNavigationTimeout(timeout)](#browsercontextsetdefaultnavigationtimeouttimeout), [browserContext.setDefaultTimeout(timeout)](#browsercontextsetdefaulttimeouttimeout), [page.setDefaultNavigationTimeout(timeout)](#pagesetdefaultnavigationtimeouttimeout) or [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) methods.
2375-
- `waitUntil` <"load"|"domcontentloaded"|"networkidle0"|"networkidle2"> When to consider navigation succeeded, defaults to `load`. Events can be either:
2376-
- `'load'` - consider navigation to be finished when the `load` event is fired.
2377-
- `'domcontentloaded'` - consider navigation to be finished when the `DOMContentLoaded` event is fired.
2378-
- `'networkidle0'` - consider navigation to be finished when there are no more than 0 network connections for at least `500` ms.
2379-
- `'networkidle2'` - consider navigation to be finished when there are no more than 2 network connections for at least `500` ms.
2380-
- returns: <[Promise]> Promise which resolves when the load state has been achieved.
2381+
#### frame.waitForLoadState([state[, options]])
2382+
- `state` <"load"|"domcontentloaded"|"networkidle0"|"networkidle2"> Load state to wait for, defaults to `load`. If the state has been already reached while loading current document, the method resolves immediately.
2383+
- `'load'` - wait for the `load` event to be fired.
2384+
- `'domcontentloaded'` - wait for the `DOMContentLoaded` event to be fired.
2385+
- `'networkidle0'` - wait until there are no more than 0 network connections for at least `500` ms.
2386+
- `'networkidle2'` - wait until there are no more than 2 network connections for at least `500` ms.
2387+
- `options` <[Object]>
2388+
- `timeout` <[number]> Maximum waiting time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the [browserContext.setDefaultNavigationTimeout(timeout)](#browsercontextsetdefaultnavigationtimeouttimeout), [browserContext.setDefaultTimeout(timeout)](#browsercontextsetdefaulttimeouttimeout), [page.setDefaultNavigationTimeout(timeout)](#pagesetdefaultnavigationtimeouttimeout) or [page.setDefaultTimeout(timeout)](#pagesetdefaulttimeouttimeout) methods.
2389+
- returns: <[Promise]> Promise which resolves when the required load state has been reached.
23812390

2382-
This resolves when the page reaches a required load state, `load` by default. The navigation can be in progress when it is called.
2383-
If navigation is already at a required state, resolves immediately.
2391+
This resolves when the frame reaches a required load state, `load` by default. The navigation must have been committed when this method is called. If current document has already reached the required state, resolves immediately.
23842392

23852393
```js
23862394
await frame.click('button'); // Click triggers navigation.
2387-
await frame.waitForLoadState(); // The promise resolves after navigation has finished.
2395+
await frame.waitForLoadState(); // The promise resolves after 'load' event.
23882396
```
23892397

23902398
#### frame.waitForNavigation([options])

src/frames.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export class Frame {
370370
await sameDocumentPromise;
371371
}
372372
const request = (navigateResult && navigateResult.newDocumentId) ? frameTask.request(navigateResult.newDocumentId) : null;
373-
await frameTask.waitForLifecycle(options.waitUntil);
373+
await frameTask.waitForLifecycle(options.waitUntil === undefined ? 'load' : options.waitUntil);
374374
frameTask.done();
375375
return request ? request._finalRequest().response() : null;
376376
}
@@ -383,14 +383,14 @@ export class Frame {
383383
frameTask.waitForSameDocumentNavigation(options.url),
384384
]);
385385
const request = documentId ? frameTask.request(documentId) : null;
386-
await frameTask.waitForLifecycle(options.waitUntil);
386+
await frameTask.waitForLifecycle(options.waitUntil === undefined ? 'load' : options.waitUntil);
387387
frameTask.done();
388388
return request ? request._finalRequest().response() : null;
389389
}
390390

391-
async waitForLoadState(options: types.NavigateOptions = {}): Promise<void> {
391+
async waitForLoadState(state: types.LifecycleEvent = 'load', options: types.TimeoutOptions = {}): Promise<void> {
392392
const frameTask = new FrameTask(this, options);
393-
await frameTask.waitForLifecycle(options.waitUntil);
393+
await frameTask.waitForLifecycle(state);
394394
frameTask.done();
395395
}
396396

@@ -507,7 +507,7 @@ export class Frame {
507507
this._page._frameManager._consoleMessageTags.set(tag, () => {
508508
// Clear lifecycle right after document.open() - see 'tag' below.
509509
this._page._frameManager.clearFrameLifecycle(this);
510-
this.waitForLoadState(options).then(resolve).catch(reject);
510+
this.waitForLoadState(options ? options.waitUntil : 'load', options).then(resolve).catch(reject);
511511
});
512512
});
513513
const contentPromise = context.evaluateInternal(({ html, tag }) => {
@@ -1078,7 +1078,7 @@ export class FrameTask {
10781078
}));
10791079
}
10801080

1081-
waitForLifecycle(waitUntil: types.LifecycleEvent = 'load'): Promise<void> {
1081+
waitForLifecycle(waitUntil: types.LifecycleEvent): Promise<void> {
10821082
if (!types.kLifecycleEvents.has(waitUntil))
10831083
throw new Error(`Unsupported waitUntil option ${String(waitUntil)}`);
10841084
return this.raceAgainstFailures(new Promise((resolve, reject) => {

src/page.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ export class Page extends platform.EventEmitter {
295295
return waitPromise;
296296
}
297297

298-
async waitForLoadState(options?: types.NavigateOptions): Promise<void> {
299-
return this.mainFrame().waitForLoadState(options);
298+
async waitForLoadState(state?: types.LifecycleEvent, options?: types.TimeoutOptions): Promise<void> {
299+
return this.mainFrame().waitForLoadState(state, options);
300300
}
301301

302302
async waitForNavigation(options?: types.WaitForNavigationOptions): Promise<network.Response | null> {

test/browsercontext.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
497497
context.waitForEvent('page'),
498498
page.evaluate(url => window.open(url), server.EMPTY_PAGE)
499499
]);
500-
await otherPage.waitForLoadState({ waitUntil: 'domcontentloaded' });
500+
await otherPage.waitForLoadState('domcontentloaded');
501501
expect(otherPage.url()).toBe(server.EMPTY_PAGE);
502502
await context.close();
503503
});
@@ -508,7 +508,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
508508
context.waitForEvent('page'),
509509
page.evaluate(url => window.open(url), 'about:blank')
510510
]);
511-
await otherPage.waitForLoadState({ waitUntil: 'domcontentloaded' });
511+
await otherPage.waitForLoadState('domcontentloaded');
512512
expect(otherPage.url()).toBe('about:blank');
513513
await context.close();
514514
});
@@ -519,7 +519,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
519519
context.waitForEvent('page'),
520520
page.evaluate(() => window.open())
521521
]);
522-
await otherPage.waitForLoadState({ waitUntil: 'domcontentloaded' });
522+
await otherPage.waitForLoadState('domcontentloaded');
523523
expect(otherPage.url()).toBe('about:blank');
524524
await context.close();
525525
});
@@ -577,7 +577,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
577577
// Issue a redirect.
578578
serverResponse.writeHead(302, { location: '/injectedstyle.css' });
579579
serverResponse.end();
580-
await newPage.waitForLoadState({ waitUntil: 'domcontentloaded' });
580+
await newPage.waitForLoadState('domcontentloaded');
581581
expect(newPage.url()).toBe(server.PREFIX + '/one-style.html');
582582
// Cleanup.
583583
await context.close();

test/elementhandle.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ module.exports.describe = function({testRunner, expect, FFOX, CHROMIUM, WEBKIT})
218218
return div;
219219
});
220220
expect(await divHandle.ownerFrame()).toBe(page.mainFrame());
221-
await popup.waitForLoadState({ waitUntil: 'domcontentloaded' });
221+
await popup.waitForLoadState('domcontentloaded');
222222
await page.evaluate(() => {
223223
const div = document.querySelector('div');
224224
window.__popup.document.body.appendChild(div);

test/emulation.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
351351
page.waitForEvent('popup'),
352352
page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'),
353353
]);
354-
await popup.waitForLoadState({ waitUntil: 'domcontentloaded' });
354+
await popup.waitForLoadState('domcontentloaded');
355355
const result = await popup.evaluate(() => window.result);
356356
expect(result).toBe('1 000 000,5');
357357
await context.close();
@@ -364,7 +364,7 @@ module.exports.describe = function({testRunner, expect, playwright, headless, FF
364364
page.waitForEvent('popup'),
365365
page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'),
366366
]);
367-
await popup.waitForLoadState({ waitUntil: 'domcontentloaded' });
367+
await popup.waitForLoadState('domcontentloaded');
368368
const result = await popup.evaluate(() => window.initialNavigatorLanguage);
369369
expect(result).toBe('fr-CH');
370370
await context.close();

0 commit comments

Comments
 (0)