2

我正在使用 Jasmine 和 PhantomJS 来运行测试用例。

在我的典型测试用例中,我拨打服务电话,等待响应并确认响应。

有些请求可能会在几秒钟内返回,有些可能需要一分钟才能返回。

当通过 PhantomJS 运行时,测试用例对于应该需要一分钟的服务调用失败(因为尚未收到响应而失败)。

有趣的是,当通过 Firefox 运行时,测试通过了。

我尝试查看 tcpdump 并且通过两个浏览器的请求的标头相同,因此这看起来像是浏览器超时问题。

有没有人有类似的问题?关于可以在哪里配置超时的任何想法?还是您认为问题出在其他地方?

4

2 回答 2

1

我有完全相同的问题。您所要做的就是添加 setTimeout 退出

 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
 });
于 2014-10-01T08:48:50.483 回答
1

啊,PhantomJS 的痛苦。

显然,我使用的bind是 PhantomJS 不支持的 javascript 函数。这导致测试失败,导致一些全局变量的状态混乱(我的错),因此失败。

但根本原因是使用bind.

bind解决方案:尝试从 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;
  };
}
于 2013-05-31T21:18:54.510 回答