Implement frame switching api in phantom shim

This patch implements frame switching api in phantom shim and
passes the relevant test.

This makes sure that puppeteer's frames API is comprehensive.

Fixes #4.
This commit is contained in:
Andrey Lushnikov 2017-07-06 14:51:34 -07:00
parent 19a8d74d3c
commit 3d3e8dd038
2 changed files with 83 additions and 2 deletions

View File

@ -49,6 +49,8 @@ class WebPage {
this._onInitialized = undefined;
this._deferEvaluate = false;
this._currentFrame = this._page.mainFrame();
this.libraryPath = path.dirname(scriptPath);
this._onResourceRequestedCallback = undefined;
@ -67,6 +69,86 @@ class WebPage {
this._pageEvents.on(PageEvents.PageError, error => (this._onError || noop).call(null, error.message, error.stack));
}
/**
* @return {string}
*/
get frameName() {
return this._currentFrame.name();
}
/**
* @return {number}
*/
get framesCount() {
return this._currentFrame.childFrames().length;
}
/**
* @return {!Array<string>}
*/
get framesName() {
return this._currentFrame.childFrames().map(frame => frame.name());
}
/**
* @return {string}
*/
get focusedFrameName() {
let focusedFrame = this._focusedFrame();
return focusedFrame ? focusedFrame.name() : '';
}
/**
* @return {?Frame}
*/
_focusedFrame() {
let frames = this._currentFrame.childFrames().slice();
frames.push(this._currentFrame);
let promises = frames.map(frame => frame.evaluate(() => document.hasFocus()));
let result = await(Promise.all(promises));
for (let i = 0; i < result.length; ++i) {
if (result[i])
return frames[i];
}
return null;
}
switchToFocusedFrame() {
let frame = this._focusedFrame();
this._currentFrame = frame;
}
/**
* @param {(string|number)} frameName
* @return {boolean}
*/
switchToFrame(frameName) {
let frame = null;
if (typeof frameName === 'string')
frame = this._currentFrame.childFrames().find(frame => frame.name() === frameName);
else if (typeof frameName === 'number')
frame = this._currentFrame.childFrames()[frameName];
if (!frame)
return false;
this._currentFrame = frame;
return true;
}
/**
* @return {boolean}
*/
switchToParentFrame() {
let frame = this._currentFrame.parentFrame();
if (!frame)
return false;
this._currentFrame = frame;
return true;
}
switchToMainFrame() {
this._currentFrame = this._page.mainFrame();
}
get onInitialized() {
return this._onInitialized;
}
@ -294,7 +376,7 @@ class WebPage {
evaluate(fun, ...args) {
if (this._deferEvaluate)
return await(this._page.evaluateOnInitialized(fun, ...args));
return await(this._page.evaluate(fun, ...args));
return await(this._currentFrame.evaluate(fun, ...args));
}
/**

View File

@ -1,4 +1,3 @@
//! unsupported
async_test(function () {
var p = require("webpage").create();