我使用带有PhantomJS的Jasmine来运行测试用例。
在我的典型测试用例中,我进行服务调用,等待响应并确认响应。
有些请求可能在几秒钟内返回,有些请求可能需要长达一分钟的时间才能返回。
当通过PhantomJS运行时,对于应该花费一分钟的服务调用,测试用例将失败(失败是因为还没有收到响应)。
有趣的是,当通过Firefox运行时,测试通过了。
我尝试过查看tcpdump,对于通过两个浏览器的请求,头部是相同的,所以这看起来像是浏览器超时问题。
有没有人遇到过类似的问题?关于在哪里可以配置超时,有什么想法吗?或者你认为问题出在别的地方?
发布于 2013-06-01 05:18:55
啊,PhantomJS的痛苦。
显然,我使用的是PhantomJS不支持的javascript的bind函数。这导致测试失败,从而导致某些全局变量的状态混乱(我的错误),从而导致失败。
但根本原因是使用了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;
};
}发布于 2014-10-01 16:48:50
我也有同样的问题。您所要做的就是添加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
});https://stackoverflow.com/questions/16846506
复制相似问题