From 4372674c993ac0af60cb6ed5178e2888771768fd Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 5 Jul 2017 10:07:36 +0300 Subject: [PATCH] Improve Phantom_Shim to handle more scenarios. (#49) This patch improves phantom_shim: - introduce WebPage.loading/WebPage.loadingProgress - improve compatibility of WebPage.onInitialized. This allows to pass phantomjs tests, even though the implementation is hacky. - teach PhantomResponse about "stage" state which could be either "start" or "end" - unskip a bunch of phantom webpage tests: - webpage/change-request-encoded-url - webpage/loading.js - webpage/long-running-javascript.js - webpage/modify-header.js - webpage/on-initialized.js - webpage/remove-header.js --- phantom_shim/WebPage.js | 38 ++++++++++++++++--- .../webpage/change-request-encoded-url.js | 1 - .../phantomjs/test/module/webpage/loading.js | 1 - .../module/webpage/long-running-javascript.js | 1 - .../test/module/webpage/modify-header.js | 1 - .../test/module/webpage/on-initialized.js | 1 - .../test/module/webpage/remove-header.js | 1 - 7 files changed, 33 insertions(+), 11 deletions(-) diff --git a/phantom_shim/WebPage.js b/phantom_shim/WebPage.js index bdca84a4c63..b9bd3e0b3e8 100644 --- a/phantom_shim/WebPage.js +++ b/phantom_shim/WebPage.js @@ -39,12 +39,14 @@ class WebPage { if (options.viewportSize) await(this._page.setViewportSize(options.viewportSize)); + this.loading = false; + this.loadingProgress = 0; this.clipRect = options.clipRect || {left: 0, top: 0, width: 0, height: 0}; this.onConsoleMessage = null; this.onLoadFinished = null; this.onResourceError = null; this.onResourceReceived = null; - this.onInitialized = null; + this._onInitialized = undefined; this._deferEvaluate = false; this.libraryPath = path.dirname(scriptPath); @@ -56,6 +58,7 @@ class WebPage { this._pageEvents = new AsyncEmitter(this._page); this._pageEvents.on(PageEvents.Response, response => this._onResponseReceived(response)); + this._pageEvents.on(PageEvents.RequestFinished, request => this._onRequestFinished(request)); this._pageEvents.on(PageEvents.RequestFailed, event => (this.onResourceError || noop).call(null, event)); this._pageEvents.on(PageEvents.ConsoleMessage, msg => (this.onConsoleMessage || noop).call(null, msg)); this._pageEvents.on(PageEvents.Confirm, message => this._onConfirm(message)); @@ -64,6 +67,17 @@ class WebPage { this._pageEvents.on(PageEvents.Error, error => (this._onError || noop).call(null, error.message, error.stack)); } + get onInitialized() { + return this._onInitialized; + } + + set onInitialized(value) { + if (typeof value !== 'function') + this._onInitialized = undefined; + else + this._onInitialized = value; + } + /** * @return {?function(!Object, !Request)} */ @@ -95,7 +109,14 @@ class WebPage { _onResponseReceived(response) { if (!this.onResourceReceived) return; - let phantomResponse = new PhantomResponse(response); + let phantomResponse = new PhantomResponse(response, false /* isResponseFinished */); + this.onResourceReceived.call(null, phantomResponse); + } + + _onRequestFinished(request) { + if (!this.onResourceReceived) + return; + let phantomResponse = new PhantomResponse(request.response(), true /* isResponseFinished */); this.onResourceReceived.call(null, phantomResponse); } @@ -236,10 +257,14 @@ class WebPage { open(url, callback) { console.assert(arguments.length <= 2, 'WebPage.open does not support METHOD and DATA arguments'); this._deferEvaluate = true; - if (typeof this.onInitialized === 'function') - this.onInitialized(); + if (typeof this._onInitialized === 'function') + this._onInitialized(); this._deferEvaluate = false; + this.loading = true; + this.loadingProgress = 50; this._page.navigate(url).then(result => { + this.loadingProgress = 100; + this.loading = false; let status = result ? 'success' : 'fail'; if (!result) { this.onResourceError.call(null, { @@ -251,6 +276,7 @@ class WebPage { this.onLoadFinished.call(null, status); if (callback) callback.call(null, status); + this.loadingProgress = 0; }); } @@ -362,11 +388,13 @@ class PhantomRequest { class PhantomResponse { /** * @param {!Response} response + * @param {boolean} isResponseFinished */ - constructor(response) { + constructor(response, isResponseFinished) { this.url = response.url; this.status = response.status; this.statusText = response.statusText; + this.stage = isResponseFinished ? 'end' : 'start'; this.headers = []; for (let entry of response.headers.entries()) { this.headers.push({ diff --git a/third_party/phantomjs/test/module/webpage/change-request-encoded-url.js b/third_party/phantomjs/test/module/webpage/change-request-encoded-url.js index b6d5327b280..37778a73e6c 100644 --- a/third_party/phantomjs/test/module/webpage/change-request-encoded-url.js +++ b/third_party/phantomjs/test/module/webpage/change-request-encoded-url.js @@ -1,4 +1,3 @@ -//! unsupported var webpage = require('webpage'); async_test(function () { diff --git a/third_party/phantomjs/test/module/webpage/loading.js b/third_party/phantomjs/test/module/webpage/loading.js index 52cc7d4420c..6af3324429d 100644 --- a/third_party/phantomjs/test/module/webpage/loading.js +++ b/third_party/phantomjs/test/module/webpage/loading.js @@ -1,4 +1,3 @@ -//! unsupported async_test(function () { var webpage = require('webpage'); var page = webpage.create(); diff --git a/third_party/phantomjs/test/module/webpage/long-running-javascript.js b/third_party/phantomjs/test/module/webpage/long-running-javascript.js index fa07bafbc7a..e8ce46a9c01 100644 --- a/third_party/phantomjs/test/module/webpage/long-running-javascript.js +++ b/third_party/phantomjs/test/module/webpage/long-running-javascript.js @@ -1,4 +1,3 @@ -//! unsupported async_test(function () { var page = require('webpage').create(); diff --git a/third_party/phantomjs/test/module/webpage/modify-header.js b/third_party/phantomjs/test/module/webpage/modify-header.js index b37d2473261..b7ba7814a5f 100644 --- a/third_party/phantomjs/test/module/webpage/modify-header.js +++ b/third_party/phantomjs/test/module/webpage/modify-header.js @@ -1,4 +1,3 @@ -//! unsupported async_test(function () { var webpage = require('webpage'); diff --git a/third_party/phantomjs/test/module/webpage/on-initialized.js b/third_party/phantomjs/test/module/webpage/on-initialized.js index a32d1f58fb0..3f8c735c118 100644 --- a/third_party/phantomjs/test/module/webpage/on-initialized.js +++ b/third_party/phantomjs/test/module/webpage/on-initialized.js @@ -1,4 +1,3 @@ -//! unsupported test(function () { var page = require('webpage').create(); diff --git a/third_party/phantomjs/test/module/webpage/remove-header.js b/third_party/phantomjs/test/module/webpage/remove-header.js index 24a4519969a..900a870fe2d 100644 --- a/third_party/phantomjs/test/module/webpage/remove-header.js +++ b/third_party/phantomjs/test/module/webpage/remove-header.js @@ -1,4 +1,3 @@ -//! unsupported var webpage = require('webpage'); // NOTE: HTTP header names are case-insensitive. Our test server