diff --git a/docs/api.md b/docs/api.md index 03095cad339..f1f209d67bf 100644 --- a/docs/api.md +++ b/docs/api.md @@ -125,12 +125,14 @@ * [frame.addScriptTag(options)](#frameaddscripttagoptions) * [frame.addStyleTag(options)](#frameaddstyletagoptions) * [frame.childFrames()](#framechildframes) + * [frame.content()](#framecontent) * [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args) * [frame.executionContext()](#frameexecutioncontext) * [frame.isDetached()](#frameisdetached) * [frame.name()](#framename) * [frame.parentFrame()](#frameparentframe) * [frame.select(selector, ...values)](#frameselectselector-values) + * [frame.setContent(html)](#framesetcontenthtml) * [frame.title()](#frametitle) * [frame.url()](#frameurl) * [frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#framewaitforselectororfunctionortimeout-options-args) @@ -1484,6 +1486,11 @@ Adds a `` tag into the page with the desired url or a `> +#### frame.content() +- returns: <[Promise]<[String]>> + +Gets the full HTML contents of the frame, including the doctype. + #### frame.evaluate(pageFunction, ...args) - `pageFunction` <[function]|[string]> Function to be evaluated in browser context - `...args` <...[Serializable]|[ElementHandle]> Arguments to pass to `pageFunction` @@ -1546,6 +1553,10 @@ frame.select('select#colors', 'blue'); // single selection frame.select('select#colors', 'red', 'green', 'blue'); // multiple selections ``` +#### frame.setContent(html) +- `html` <[string]> HTML markup to assign to the page. +- returns: <[Promise]> + #### frame.title() - returns: <[Promise]<[string]>> Returns page's title. diff --git a/lib/FrameManager.js b/lib/FrameManager.js index 3dda7d13474..7de7d0c170a 100644 --- a/lib/FrameManager.js +++ b/lib/FrameManager.js @@ -339,6 +339,31 @@ class Frame { return result; } + /** + * @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 + */ + async setContent(html) { + await this.evaluate(html => { + document.open(); + document.write(html); + document.close(); + }, html); + } + /** * @return {string} */ diff --git a/lib/Page.js b/lib/Page.js index 1b9ecda6792..8adbafbc57d 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -428,25 +428,14 @@ class Page extends EventEmitter { * @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; - }); + return await this._frameManager.mainFrame().content(); } /** * @param {string} html */ async setContent(html) { - await this.evaluate(html => { - document.open(); - document.write(html); - document.close(); - }, html); + await this._frameManager.mainFrame().setContent(html); } /**