【问题标题】:PhantomJs timeoutPhantomJs 超时
【发布时间】:2014-10-01 17:00:10
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

    标签: timeout jasmine phantomjs


    【解决方案1】:

    啊,PhantomJS 的痛苦。

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

    但根本原因是使用bind

    解决方案:尝试从 https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind 获取像这样的 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】:

      我遇到了完全相同的问题。您所要做的就是添加 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
       });
      

      【讨论】:

        猜你喜欢
        • 2013-05-27
        • 1970-01-01
        • 2015-01-12
        • 2012-03-11
        • 2017-04-16
        • 2017-07-20
        • 1970-01-01
        • 2016-06-14
        • 1970-01-01
        相关资源
        最近更新 更多