From ef369e5d5d30f8b09ff18ffabba99ac759b7c4c7 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 24 Oct 2017 00:35:18 -0700 Subject: [PATCH] feat(Page.goto): add a 'domcontentloaded' option to the Page.goto (#1145) This patch adds a new `domcontentloaded` option to a bunch of navigation methods: - Page.goto - Page.waitForNavigation - Page.goBack - Page.goForward - Page.reload Fixes #946. --- docs/api.md | 7 ++++++- lib/NavigatorWatcher.js | 5 +++-- test/test.js | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/api.md b/docs/api.md index 2c37c613..63fa85c1 100644 --- a/docs/api.md +++ b/docs/api.md @@ -763,6 +763,7 @@ If there's no element matching `selector`, the method throws an error. - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - `waitUntil` <[string]> When to consider a navigation finished, defaults to `load`. Can be either: - `load` - consider navigation to be finished when the `load` event is fired. + - `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired. - `networkidle0` - consider navigation to be finished when there are no more then 0 network connections for at least `500` ms. - `networkidle2` - consider navigation to be finished when there are no more then 2 network connections for at least `500` ms. - returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If @@ -775,6 +776,7 @@ Navigate to the previous page in history. - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - `waitUntil` <[string]> When to consider navigation succeeded, defaults to `load`. Can be either: - `load` - consider navigation to be finished when the `load` event is fired. + - `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired. - `networkidle0` - consider navigation to be finished when there are no more then 0 network connections for at least `500` ms. - `networkidle2` - consider navigation to be finished when there are no more then 2 network connections for at least `500` ms. - returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If @@ -788,6 +790,7 @@ Navigate to the next page in history. - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - `waitUntil` <[string]> When to consider navigation succeeded, defaults to `load`. Can be either: - `load` - consider navigation to be finished when the `load` event is fired. + - `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired. - `networkidle0` - consider navigation to be finished when there are no more then 0 network connections for at least `500` ms. - `networkidle2` - consider navigation to be finished when there are no more then 2 network connections for at least `500` ms. - returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. @@ -902,6 +905,7 @@ Shortcut for [page.mainFrame().executionContext().queryObjects(prototypeHandle)] - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - `waitUntil` <[string]> When to consider navigation succeeded, defaults to `load`. Can be either: - `load` - consider navigation to be finished when the `load` event is fired. + - `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired. - `networkidle0` - consider navigation to be finished when there are no more then 0 network connections for at least `500` ms. - `networkidle2` - consider navigation to be finished when there are no more then 2 network connections for at least `500` ms. - returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. @@ -1105,6 +1109,7 @@ Shortcut for [page.mainFrame().waitForFunction(pageFunction[, options[, ...args] - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - `waitUntil` <[string]> When to consider navigation succeeded, defaults to `load`. Can be either: - `load` - consider navigation to be finished when the `load` event is fired. + - `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired. - `networkidle0` - consider navigation to be finished when there are no more then 0 network connections for at least `500` ms. - `networkidle2` - consider navigation to be finished when there are no more then 2 network connections for at least `500` ms. - returns: <[Promise]<[Response]>> Promise which resolves to the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. @@ -2017,4 +2022,4 @@ Identifies what kind of target this is. Can be `"page"`, `"service_worker"`, or [Serializable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable" [Touchscreen]: #class-touchscreen "Touchscreen" [Target]: #class-target "Target" -[USKeyboardLayout]: ../lib/USKeyboardLayout.js "USKeyboardLayout" \ No newline at end of file +[USKeyboardLayout]: ../lib/USKeyboardLayout.js "USKeyboardLayout" diff --git a/lib/NavigatorWatcher.js b/lib/NavigatorWatcher.js index 25d9a693..f48dfb9b 100644 --- a/lib/NavigatorWatcher.js +++ b/lib/NavigatorWatcher.js @@ -32,7 +32,7 @@ class NavigatorWatcher { this._ignoreHTTPSErrors = ignoreHTTPSErrors; this._timeout = typeof options['timeout'] === 'number' ? options['timeout'] : 30000; const waitUntil = typeof options['waitUntil'] === 'string' ? options['waitUntil'] : 'load'; - const isAllowedWaitUntil = waitUntil === 'networkidle0' || waitUntil === 'networkidle2' || waitUntil === 'load'; + const isAllowedWaitUntil = waitUntil === 'networkidle0' || waitUntil === 'networkidle2' || waitUntil === 'load' || waitUntil === 'domcontentloaded'; console.assert(isAllowedWaitUntil, 'Unknown value for options.waitUntil: ' + waitUntil); this._pendingEvents = new Set([waitUntil]); } @@ -92,8 +92,9 @@ class NavigatorWatcher { const protocolLifecycleToPuppeteer = { 'load': 'load', + 'DOMContentLoaded': 'domcontentloaded', 'networkIdle': 'networkidle0', - 'networkAlmostIdle': 'networkidle2' + 'networkAlmostIdle': 'networkidle2', }; module.exports = NavigatorWatcher; diff --git a/test/test.js b/test/test.js index 37e3be27..b92951c7 100644 --- a/test/test.js +++ b/test/test.js @@ -884,6 +884,10 @@ describe('Page', function() { const response = await page.goto('about:blank'); expect(response).toBe(null); })); + it('should navigate to empty page with domcontentloaded', SX(async function() { + const response = await page.goto(EMPTY_PAGE, {waitUntil: 'domcontentloaded'}); + expect(response.status).toBe(200); + })); it('should navigate to empty page with networkidle0', SX(async function() { const response = await page.goto(EMPTY_PAGE, {waitUntil: 'networkidle0'}); expect(response.status).toBe(200);