feat(Frame): implement frame.content and frame.setContent methods (#1447)

This refactors the page.content and page.setContent methods to be defined on the Frame class. This allows access from the Page still but also on all frames.

Fixes #754
This commit is contained in:
Trent Willis 2017-11-22 18:44:33 -08:00 committed by Andrey Lushnikov
parent 3b60ad1c39
commit 770c17b2ea
3 changed files with 38 additions and 13 deletions

View File

@ -125,12 +125,14 @@
* [frame.addScriptTag(options)](#frameaddscripttagoptions) * [frame.addScriptTag(options)](#frameaddscripttagoptions)
* [frame.addStyleTag(options)](#frameaddstyletagoptions) * [frame.addStyleTag(options)](#frameaddstyletagoptions)
* [frame.childFrames()](#framechildframes) * [frame.childFrames()](#framechildframes)
* [frame.content()](#framecontent)
* [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args) * [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args)
* [frame.executionContext()](#frameexecutioncontext) * [frame.executionContext()](#frameexecutioncontext)
* [frame.isDetached()](#frameisdetached) * [frame.isDetached()](#frameisdetached)
* [frame.name()](#framename) * [frame.name()](#framename)
* [frame.parentFrame()](#frameparentframe) * [frame.parentFrame()](#frameparentframe)
* [frame.select(selector, ...values)](#frameselectselector-values) * [frame.select(selector, ...values)](#frameselectselector-values)
* [frame.setContent(html)](#framesetcontenthtml)
* [frame.title()](#frametitle) * [frame.title()](#frametitle)
* [frame.url()](#frameurl) * [frame.url()](#frameurl)
* [frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#framewaitforselectororfunctionortimeout-options-args) * [frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#framewaitforselectororfunctionortimeout-options-args)
@ -1484,6 +1486,11 @@ Adds a `<link rel="stylesheet">` tag into the page with the desired url or a `<s
#### frame.childFrames() #### frame.childFrames()
- returns: <[Array]<[Frame]>> - returns: <[Array]<[Frame]>>
#### frame.content()
- returns: <[Promise]<[String]>>
Gets the full HTML contents of the frame, including the doctype.
#### frame.evaluate(pageFunction, ...args) #### frame.evaluate(pageFunction, ...args)
- `pageFunction` <[function]|[string]> Function to be evaluated in browser context - `pageFunction` <[function]|[string]> Function to be evaluated in browser context
- `...args` <...[Serializable]|[ElementHandle]> Arguments to pass to `pageFunction` - `...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.select('select#colors', 'red', 'green', 'blue'); // multiple selections
``` ```
#### frame.setContent(html)
- `html` <[string]> HTML markup to assign to the page.
- returns: <[Promise]>
#### frame.title() #### frame.title()
- returns: <[Promise]<[string]>> Returns page's title. - returns: <[Promise]<[string]>> Returns page's title.

View File

@ -339,6 +339,31 @@ class Frame {
return result; return result;
} }
/**
* @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
*/
async setContent(html) {
await this.evaluate(html => {
document.open();
document.write(html);
document.close();
}, html);
}
/** /**
* @return {string} * @return {string}
*/ */

View File

@ -428,25 +428,14 @@ class Page extends EventEmitter {
* @return {!Promise<String>} * @return {!Promise<String>}
*/ */
async content() { async content() {
return await this.evaluate(() => { return await this._frameManager.mainFrame().content();
let retVal = '';
if (document.doctype)
retVal = new XMLSerializer().serializeToString(document.doctype);
if (document.documentElement)
retVal += document.documentElement.outerHTML;
return retVal;
});
} }
/** /**
* @param {string} html * @param {string} html
*/ */
async setContent(html) { async setContent(html) {
await this.evaluate(html => { await this._frameManager.mainFrame().setContent(html);
document.open();
document.write(html);
document.close();
}, html);
} }
/** /**