diff --git a/docs/api.md b/docs/api.md index 8e77d8a5..9740dc08 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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]> diff --git a/lib/Page.js b/lib/Page.js index d9f54bf1..fd30b0e9 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -260,6 +260,20 @@ class Page extends EventEmitter { return this.mainFrame().url(); } + /** + * @return {!Promise} + */ + 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} diff --git a/test/test.js b/test/test.js index d04680b4..9f473a04 100644 --- a/test/test.js +++ b/test/test.js @@ -1437,10 +1437,24 @@ describe('Page', function() { })); }); describe('Page.setContent', function() { + const expectedOutput = '
hello
'; it('should work', SX(async function() { await page.setContent('
hello
'); - let result = await page.evaluate(() => document.body.innerHTML); - expect(result).toBe('
hello
'); + let result = await page.content(); + expect(result).toBe(expectedOutput); + })); + it('should work with doctype', SX(async function() { + const doctype = ''; + await page.setContent(`${doctype}
hello
`); + let result = await page.content(); + expect(result).toBe(`${doctype}${expectedOutput}`); + })); + it('should work with HTML 4 doctype', SX(async function() { + const doctype = ''; + await page.setContent(`${doctype}
hello
`); + let result = await page.content(); + expect(result).toBe(`${doctype}${expectedOutput}`); })); }); describe('Network Events', function() {