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
This commit is contained in:
Andrey Lushnikov 2017-07-05 10:07:36 +03:00 committed by GitHub
parent 2cd60c9e1e
commit 4372674c99
7 changed files with 33 additions and 11 deletions

View File

@ -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({

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,3 @@
//! unsupported
test(function () {
var page = require('webpage').create();

View File

@ -1,4 +1,3 @@
//! unsupported
var webpage = require('webpage');
// NOTE: HTTP header names are case-insensitive. Our test server