Skip to content

Commit

Permalink
Add Page.content() method. Fixes puppeteer#406. (puppeteer#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig authored and aslushnikov committed Aug 21, 2017
1 parent 6fcf3d9 commit 598f439
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
+ [page.addScriptTag(url)](#pageaddscripttagurl)
+ [page.click(selector[, options])](#pageclickselector-options)
+ [page.close()](#pageclose)
+ [page.content()](#pagecontent)
+ [page.emulate(options)](#pageemulateoptions)
+ [page.emulateMedia(mediaType)](#pageemulatemediamediatype)
+ [page.evaluate(pageFunction, ...args)](#pageevaluatepagefunction-args)
Expand Down Expand Up @@ -317,6 +318,11 @@ If there's no element matching `selector`, the method throws an error.
#### page.close()
- returns: <[Promise]>

#### page.content()
- returns: <[Promise]<[String]>>

Gets the full HTML contents of the page, including the doctype.

#### page.emulate(options)
- `options` <[Object]>
- `viewport` <[Object]>
Expand Down
14 changes: 14 additions & 0 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ class Page extends EventEmitter {
return this.mainFrame().url();
}

/**
* @return {!Promise<String>}
*/
async content() {
return await this.evaluate(() => {
let retVal = '';
if (document.doctype)
retVal = new XMLSerializer().serializeToString(document.doctype);
if (document.documentElement)
retVal += document.documentElement.outerHTML;
return retVal;
});
}

/**
* @param {string} html
* @return {!Promise}
Expand Down
18 changes: 16 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1437,10 +1437,24 @@ describe('Page', function() {
}));
});
describe('Page.setContent', function() {
const expectedOutput = '<html><head></head><body><div>hello</div></body></html>';
it('should work', SX(async function() {
await page.setContent('<div>hello</div>');
let result = await page.evaluate(() => document.body.innerHTML);
expect(result).toBe('<div>hello</div>');
let result = await page.content();
expect(result).toBe(expectedOutput);
}));
it('should work with doctype', SX(async function() {
const doctype = '<!DOCTYPE html>';
await page.setContent(`${doctype}<div>hello</div>`);
let result = await page.content();
expect(result).toBe(`${doctype}${expectedOutput}`);
}));
it('should work with HTML 4 doctype', SX(async function() {
const doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ' +
'"http://www.w3.org/TR/html4/strict.dtd">';
await page.setContent(`${doctype}<div>hello</div>`);
let result = await page.content();
expect(result).toBe(`${doctype}${expectedOutput}`);
}));
});
describe('Network Events', function() {
Expand Down

0 comments on commit 598f439

Please sign in to comment.