From 3d3e8dd03828572851e21cef7e00965ecc64e4d1 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 6 Jul 2017 14:51:34 -0700 Subject: [PATCH] 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. --- phantom_shim/WebPage.js | 84 ++++++++++++++++++- .../test/module/webpage/frame-switching.js | 1 - 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/phantom_shim/WebPage.js b/phantom_shim/WebPage.js index 37d9065b..05887a90 100644 --- a/phantom_shim/WebPage.js +++ b/phantom_shim/WebPage.js @@ -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} + */ + 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)); } /** diff --git a/third_party/phantomjs/test/module/webpage/frame-switching.js b/third_party/phantomjs/test/module/webpage/frame-switching.js index 295bf377..a8213203 100644 --- a/third_party/phantomjs/test/module/webpage/frame-switching.js +++ b/third_party/phantomjs/test/module/webpage/frame-switching.js @@ -1,4 +1,3 @@ -//! unsupported async_test(function () { var p = require("webpage").create();