From 80ee46942908054dea3bc14e6505bc12718e0b2d Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 24 Oct 2017 13:57:39 -0700 Subject: [PATCH] feat(Page): teach Page.setContent to wait for resources to load (#1152) This patch adds "options" parameter to the `page.setContent` method. The parameter is the same as a navigation parameter and allows to specify maximum timeout to wait for resources to be loaded, as well as to describe events that should be emitted before the setContent operation would be considered successful. Fixes #728. --- docs/api.md | 23 +++++++++++++++-------- lib/Page.js | 16 ++++++++++------ test/test.js | 11 +++++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/docs/api.md b/docs/api.md index 8bdbe3206d7..4751399a1e4 100644 --- a/docs/api.md +++ b/docs/api.md @@ -70,7 +70,7 @@ * [page.reload(options)](#pagereloadoptions) * [page.screenshot([options])](#pagescreenshotoptions) * [page.select(selector, ...values)](#pageselectselector-values) - * [page.setContent(html)](#pagesetcontenthtml) + * [page.setContent(html, options)](#pagesetcontenthtml-options) * [page.setCookie(...cookies)](#pagesetcookiecookies) * [page.setExtraHTTPHeaders(headers)](#pagesetextrahttpheadersheaders) * [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled) @@ -761,7 +761,7 @@ If there's no element matching `selector`, the method throws an error. #### page.goBack(options) - `options` <[Object]> Navigation parameters which might have the following properties: - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events Can be either: + - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events 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. @@ -774,7 +774,7 @@ Navigate to the previous page in history. #### page.goForward(options) - `options` <[Object]> Navigation parameters which might have the following properties: - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events Can be either: + - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events 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. @@ -788,7 +788,7 @@ Navigate to the next page in history. - `url` <[string]> URL to navigate page to. The url should include scheme, e.g. `https://`. - `options` <[Object]> Navigation parameters which might have the following properties: - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events Can be either: + - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events 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. @@ -903,7 +903,7 @@ Shortcut for [page.mainFrame().executionContext().queryObjects(prototypeHandle)] #### page.reload(options) - `options` <[Object]> Navigation parameters which might have the following properties: - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events Can be either: + - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events 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. @@ -937,9 +937,16 @@ page.select('select#colors', 'blue'); // single selection page.select('select#colors', 'red', 'green', 'blue'); // multiple selections ``` -#### page.setContent(html) +#### page.setContent(html, options) - `html` <[string]> HTML markup to assign to the page. -- returns: <[Promise]> +- `options` <[Object]> Navigation parameters which might have the following properties: + - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. + - `waitUntil` <[string]|[Array]<[string]>> When to consider setting content complete, defaults to `load`. Given an array of event strings, setting content is considered to be successful after all events have been fired. Events can be either: + - `load` - consider setting content to be finished when the `load` event is fired. + - `domcontentloaded` - consider setting content to be finished when the `DOMContentLoaded` event is fired. + - `networkidle0` - consider setting content to be finished when there are no more then 0 network connections for at least `500` ms. + - `networkidle2` - consider setting content to be finished when there are no more then 2 network connections for at least `500` ms. +- returns: <[Promise]> Promise which resolves when content is set and all events are triggered. #### page.setCookie(...cookies) - `...cookies` <...[Object]> @@ -1107,7 +1114,7 @@ Shortcut for [page.mainFrame().waitForFunction(pageFunction[, options[, ...args] #### page.waitForNavigation(options) - `options` <[Object]> Navigation parameters which might have the following properties: - `timeout` <[number]> Maximum navigation time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. - - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events Can be either: + - `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events 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. diff --git a/lib/Page.js b/lib/Page.js index dea00d9b750..9b0b7ae1537 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -437,13 +437,17 @@ class Page extends EventEmitter { /** * @param {string} html + * @param {!Object=} options */ - async setContent(html) { - await this.evaluate(html => { - document.open(); - document.write(html); - document.close(); - }, html); + async setContent(html, options) { + await Promise.all([ + this.evaluate(html => { + document.open(); + document.write(html); + document.close(); + }, html), + this.waitForNavigation(options), + ]); } /** diff --git a/test/test.js b/test/test.js index 767b51eab0d..f3566729029 100644 --- a/test/test.js +++ b/test/test.js @@ -2174,6 +2174,17 @@ describe('Page', function() { const result = await page.content(); expect(result).toBe(`${doctype}${expectedOutput}`); })); + it('should await resources to load', SX(async function() { + const imgPath = '/img.png'; + let imgResponse = null; + server.setRoute(imgPath, (req, res) => imgResponse = res); + let loaded = false; + const contentPromise = page.setContent(``).then(() => loaded = true); + await server.waitForRequest(imgPath); + expect(loaded).toBe(false); + imgResponse.end(); + await contentPromise; + })); }); describe('Network Events', function() {