Skip to content

Commit

Permalink
Improve Phantom_Shim to handle more scenarios. (puppeteer#49)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aslushnikov authored Jul 5, 2017
1 parent 2cd60c9 commit 4372674
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
38 changes: 33 additions & 5 deletions phantom_shim/WebPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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)}
*/
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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, {
Expand All @@ -251,6 +276,7 @@ class WebPage {
this.onLoadFinished.call(null, status);
if (callback)
callback.call(null, status);
this.loadingProgress = 0;
});
}

Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! unsupported
var webpage = require('webpage');

async_test(function () {
Expand Down
1 change: 0 additions & 1 deletion third_party/phantomjs/test/module/webpage/loading.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! unsupported
async_test(function () {
var webpage = require('webpage');
var page = webpage.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! unsupported
async_test(function () {
var page = require('webpage').create();

Expand Down
1 change: 0 additions & 1 deletion third_party/phantomjs/test/module/webpage/modify-header.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! unsupported
async_test(function () {
var webpage = require('webpage');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! unsupported
test(function () {
var page = require('webpage').create();

Expand Down
1 change: 0 additions & 1 deletion third_party/phantomjs/test/module/webpage/remove-header.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! unsupported
var webpage = require('webpage');

// NOTE: HTTP header names are case-insensitive. Our test server
Expand Down

0 comments on commit 4372674

Please sign in to comment.