Skip to content

Commit

Permalink
feat(Page): teach Page.setContent to wait for resources to load
Browse files Browse the repository at this point in the history
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 puppeteer#728.
  • Loading branch information
aslushnikov committed Oct 24, 2017
1 parent f38c8bb commit f6e27a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
11 changes: 9 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand Down
16 changes: 10 additions & 6 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
let contentPromise = page.setContent(`<img src="${PREFIX + imgPath}"></img>`).then(() => loaded = true);
await server.waitForRequest(imgPath);
expect(loaded).toBe(false);
imgResponse.end();
await contentPromise;
}));
});

describe('Network Events', function() {
Expand Down

0 comments on commit f6e27a9

Please sign in to comment.