Add Page.content() method. Fixes #406. (#419)

This commit is contained in:
John Resig 2017-08-21 12:02:30 -04:00 committed by Andrey Lushnikov
parent 6fcf3d9148
commit 598f439a32
3 changed files with 36 additions and 2 deletions

View File

@ -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)
@ -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]>

View File

@ -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}

View File

@ -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() {