Move missing methods from page onto frame. (#125)

Closes #113
This commit is contained in:
JoelEinbinder 2017-07-25 11:37:46 -07:00 committed by Andrey Lushnikov
parent b564029589
commit 4f5f1f6d77
3 changed files with 109 additions and 34 deletions

View File

@ -79,13 +79,17 @@
* [class: Frame](#class-frame) * [class: Frame](#class-frame)
+ [frame.$(selector, pageFunction, ...args)](#frameselector-pagefunction-args) + [frame.$(selector, pageFunction, ...args)](#frameselector-pagefunction-args)
+ [frame.$$(selector, pageFunction, ...args)](#frameselector-pagefunction-args) + [frame.$$(selector, pageFunction, ...args)](#frameselector-pagefunction-args)
+ [frame.addScriptTag(url)](#frameaddscripttagurl)
+ [frame.childFrames()](#framechildframes) + [frame.childFrames()](#framechildframes)
+ [frame.click(selector[, options])](#frameclickselector-options) + [frame.click(selector[, options])](#frameclickselector-options)
+ [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args) + [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args)
+ [frame.focus(selector)](#framefocusselector)
+ [frame.hover(selector)](#framehoverselector) + [frame.hover(selector)](#framehoverselector)
+ [frame.injectFile(filePath)](#frameinjectfilefilepath)
+ [frame.isDetached()](#frameisdetached) + [frame.isDetached()](#frameisdetached)
+ [frame.name()](#framename) + [frame.name()](#framename)
+ [frame.parentFrame()](#frameparentframe) + [frame.parentFrame()](#frameparentframe)
+ [frame.title()](#frametitle)
+ [frame.url()](#frameurl) + [frame.url()](#frameurl)
+ [frame.waitFor(selectorOrTimeout[, options])](#framewaitforselectorortimeout-options) + [frame.waitFor(selectorOrTimeout[, options])](#framewaitforselectorortimeout-options)
+ [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options) + [frame.waitForSelector(selector[, options])](#framewaitforselectorselector-options)
@ -347,7 +351,7 @@ Shortcut for [page.mainFrame().$$(selector, pageFunction, ...args)](#pageselecto
- `url` <[string]> Url of a script to be added - `url` <[string]> Url of a script to be added
- returns: <[Promise]> Promise which resolves as the script gets added and loads. - returns: <[Promise]> Promise which resolves as the script gets added and loads.
Adds a `<script></script>` tag to the page with the desired url. Alternatively, javascript could be injected to the page via `page.injectFile` method. Adds a `<script></script>` tag to the page with the desired url. Alternatively, javascript could be injected to the page via [`page.injectFile`](#pageinjectfilefilepath) method.
#### page.click(selector[, options]) #### page.click(selector[, options])
- `selector` <[string]> A query selector to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked. - `selector` <[string]> A query selector to search for element to click. If there are multiple elements satisfying the selector, the first will be clicked.
@ -791,6 +795,12 @@ browser.newPage().then(async page => {
- `...args` <...[string]> Arguments to pass to `pageFunction` - `...args` <...[string]> Arguments to pass to `pageFunction`
- returns: <[Promise]<[Array]<[Object]>>> Promise which resolves to array of function return values. - returns: <[Promise]<[Array]<[Object]>>> Promise which resolves to array of function return values.
#### frame.addScriptTag(url)
- `url` <[string]> Url of a script to be added
- returns: <[Promise]> Promise which resolves as the script gets added and loads.
Adds a `<script></script>` tag to the frame with the desired url. Alternatively, javascript could be injected to the frame via [`frame.injectFile`](#frameinjectfilefilepath) method.
#### frame.childFrames() #### frame.childFrames()
- returns: <[Array]<[Frame]>> - returns: <[Array]<[Frame]>>
@ -820,10 +830,18 @@ browser.newPage().then(async page =>
}); });
``` ```
#### frame.focus(selector)
- `selector` <[string]> A query selector of element to focus. If there are multiple elements satisfying the selector, the first will be focused.
- returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully focused. Promise gets rejected if there's no element matching `selector`.
#### frame.hover(selector) #### frame.hover(selector)
- `selector` <[string]> A query selector to search for element to hover. If there are multiple elements satisfying the selector, the first will be hovered. - `selector` <[string]> A query selector to search for element to hover. If there are multiple elements satisfying the selector, the first will be hovered.
- returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully hovered. Promise gets rejected if there's no element matching `selector`. - returns: <[Promise]> Promise which resolves when the element matching `selector` is successfully hovered. Promise gets rejected if there's no element matching `selector`.
#### frame.injectFile(filePath)
- `filePath` <[string]> Path to the javascript file to be injected into frame.
- returns: <[Promise]> Promise which resolves when file gets successfully evaluated in frame.
#### frame.isDetached() #### frame.isDetached()
- returns: <[boolean]> - returns: <[boolean]>
@ -837,6 +855,9 @@ Returns frame's name as specified in the tag.
#### frame.parentFrame() #### frame.parentFrame()
- returns: <[Frame]> Returns parent frame, if any. Detached frames and main frames return `null`. - returns: <[Frame]> Returns parent frame, if any. Detached frames and main frames return `null`.
#### frame.title()
- returns: <[Promise]<[string]>> Returns page's title.
#### frame.url() #### frame.url()
- returns: <[string]> - returns: <[string]>

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
let fs = require('fs');
let EventEmitter = require('events'); let EventEmitter = require('events');
let helper = require('./helper'); let helper = require('./helper');
@ -157,21 +158,43 @@ class FrameManager extends EventEmitter {
/** /**
* @param {!Frame} frame * @param {!Frame} frame
* @param {string} expression * @return {number}
* @return {!Promise<(!Object|undefined)>}
*/ */
async _evaluateOnFrame(frame, expression) { _contextIdForFrame(frame) {
let contextId = undefined; let contextId = undefined;
if (frame !== this._mainFrame) { if (frame !== this._mainFrame) {
contextId = this._frameIdToExecutionContextId.get(frame._id); contextId = this._frameIdToExecutionContextId.get(frame._id);
console.assert(contextId, 'Frame does not have default context to evaluate in!'); console.assert(contextId, 'Frame does not have default context to evaluate in!');
} }
return contextId;
}
/**
* @param {!Frame} frame
* @param {string} expression
* @return {!Promise<(!Object|undefined)>}
*/
async _evaluateOnFrame(frame, expression) {
let contextId = this._contextIdForFrame(frame);
expression = `Promise.resolve(${expression})`; expression = `Promise.resolve(${expression})`;
let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true }); let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true });
if (exceptionDetails) if (exceptionDetails)
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails)); throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
return await helper.serializeRemoteObject(this._client, remoteObject); return await helper.serializeRemoteObject(this._client, remoteObject);
} }
/**
* @param {!Frame} frame
* @param {string} expression
* @return {!Promise<(!Object|undefined)>}
*/
async _rawEvaluateOnFrame(frame, expression) {
let contextId = this._contextIdForFrame(frame);
let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false });
if (exceptionDetails)
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
return await helper.serializeRemoteObject(this._client, remoteObject);
}
} }
/** @enum {string} */ /** @enum {string} */
@ -251,6 +274,40 @@ class Frame {
return this._detached; return this._detached;
} }
/**
* @param {string} filePath
* @return {!Promise}
*/
async injectFile(filePath) {
let contents = await new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
contents += `//# sourceURL=` + filePath.replace(/\n/g,'');
return this._frameManager._rawEvaluateOnFrame(this, contents);
}
/**
* @param {string} url
* @return {!Promise}
*/
async addScriptTag(url) {
return this.evaluate(addScriptTag, url);
/**
* @param {string} url
*/
function addScriptTag(url) {
let script = document.createElement('script');
script.src = url;
let promise = new Promise(x => script.onload = x);
document.head.appendChild(script);
return promise;
}
}
/** /**
* @param {(string|number)} selectorOrTimeout * @param {(string|number)} selectorOrTimeout
* @param {!Object=} options * @param {!Object=} options
@ -313,6 +370,13 @@ class Frame {
return this._frameManager._evaluateOnFrame(this, expression); return this._frameManager._evaluateOnFrame(this, expression);
} }
/**
* @return {!Promise<string>}
*/
async title() {
return this.evaluate(() => document.title);
}
/** /**
* @param {string} selector * @param {string} selector
* @return {!Promise} * @return {!Promise}
@ -346,6 +410,22 @@ class Frame {
await this.evaluate(() => new Promise(f => requestAnimationFrame(f))); await this.evaluate(() => new Promise(f => requestAnimationFrame(f)));
} }
/**
* @param {string} selector
* @return {!Promise}
*/
async focus(selector) {
let success = await this.evaluate(selector => {
let node = document.querySelector(selector);
if (!node)
return false;
node.focus();
return true;
}, selector);
if (!success)
throw new Error('No node found for selector: ' + selector);
}
/** /**
* @param {?Object} framePayload * @param {?Object} framePayload
*/ */

View File

@ -119,18 +119,7 @@ class Page extends EventEmitter {
* @return {!Promise} * @return {!Promise}
*/ */
async addScriptTag(url) { async addScriptTag(url) {
return this.evaluate(addScriptTag, url); return this.mainFrame().addScriptTag(url);
/**
* @param {string} url
*/
function addScriptTag(url) {
let script = document.createElement('script');
script.src = url;
let promise = new Promise(x => script.onload = x);
document.head.appendChild(script);
return promise;
}
} }
/** /**
@ -138,14 +127,7 @@ class Page extends EventEmitter {
* @return {!Promise} * @return {!Promise}
*/ */
async injectFile(filePath) { async injectFile(filePath) {
let contents = await new Promise((resolve, reject) => { return this.mainFrame().injectFile(filePath);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
contents += `//# sourceURL=` + filePath.replace(/\n/g,'');
return this._client.send('Runtime.evaluate', { expression: contents, returnByValue: true });
} }
/** /**
@ -498,7 +480,7 @@ class Page extends EventEmitter {
* @return {!Promise<string>} * @return {!Promise<string>}
*/ */
async title() { async title() {
return this.evaluate(() => document.title); return this.mainFrame().title();
} }
/** /**
@ -552,15 +534,7 @@ class Page extends EventEmitter {
* @return {!Promise} * @return {!Promise}
*/ */
async focus(selector) { async focus(selector) {
let success = await this.evaluate(selector => { return this.mainFrame().focus(selector);
let node = document.querySelector(selector);
if (!node)
return false;
node.focus();
return true;
}, selector);
if (!success)
throw new Error('No node found for selector: ' + selector);
} }
/** /**