PhantomJs timeout

喜欢而已 提交于 2019-12-12 17:27:55

问题


I am using Jasmine with PhantomJS to run test cases.

In my typical test case, I make a service call, wait for response and confirm response.

Some requests can return in a few seconds and some can take up to a minute to return.

When ran through PhantomJS, the test case fails for the service call that is supposed to take a minute ( fails because the response is not yet received).

What's interesting is that the test passes when ran through Firefox.

I have tried looking at tcpdump and the headers are same for requests through both browsers, so this looks like a browser timeout issue.

Has anyone had a similar issue ? Any ideas as to where could the timeout be configured ? Or do you think the problem is something else ?


回答1:


Ah the pain of PhantomJS.

Apparently it turned out that I was using javascript's bind function which is not supported in PhantomJS . This was causing the test to fail which resulted in messing up state of some global variable( my fault) and hence the failure.

But the root cause was using bind.

Solution: try getting a shim for bind like this from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind

if (!Function.prototype.bind) {
  Function.prototype.bind = function (oThis) {
    if (typeof this !== "function") {
      // closest thing possible to the ECMAScript 5 internal IsCallable function
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
    }

    var aArgs = Array.prototype.slice.call(arguments, 1), 
        fToBind = this, 
        fNOP = function () {},
        fBound = function () {
          return fToBind.apply(this instanceof fNOP && oThis
                                 ? this
                                 : oThis,
                               aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;
  };
}



回答2:


I had exactly same issue. All you have to do is add setTimeout to exit

 setTimeout(function() {phantom.exit();},20000); // stop after 20 sec ( add this before you request your webpage )

 page.open('your url here', function (status) {
  // operations here
 });


来源:https://stackoverflow.com/questions/16846506/phantomjs-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!