[英]how to capture and test for socket.io events in casper.js
我正在使用Casperjs测试socket.io应用程序。 我想要断言服务器和客户端发生了不同的发射,但是我下面的代码不断给我错误。
我的测试设置中包含以下行:
casper.page.injectJs('test_include.js');
test_include.js看起来像:
document.emits = [];
document.ons = [];
(function(){
var _origEmit = main_socket.emit;
main_socket.emit = function(){
console.log("SENT", Array.prototype.slice.call(arguments));
document.emits.push(arguments);
_origEmit.apply(this, Array.prototype.slice.call(arguments));
}
var _origOn = main_socket.$emit;
main_socket.$emit = function(){
console.log("RECEIVED", Array.prototype.slice.call(arguments));
document.ons.push(arguments);
_origOn.apply(this, Array.prototype.slice.call(arguments));
}
console.log("Injection complete");
})();
注入完成运行,所以我知道injectjs在工作。 然后,在我的测试用例中,检查它是否完全正常工作:
casper.evaluate(function(){
main_socket.emit('testin');
console.log("EMITS", JSON.stringify(document.emits));
});
输出基本上显示没有发出任何信号,即使我只是发出了一些信号:
EMITS [{}]
对于我的最终目标,我希望能够单击链接,断言发出了正确的socket.io事件,等待500ms并断言服务器已经发出了我希望看到的内容。 看来这将是每天的用例,但我在使整个周期正常工作方面遇到了麻烦。
(请注意,在我的更新中,我发现了正在发生的错误,并且在其他地方,所以我进行了更新以显示什么在起作用而不是错误)
我发现了问题:
在我的test_include.js文件中,我错误地推入了阵列。
document.emits.push(Array.prototype.slice.call(arguments));
不知道为什么我只是在推论而不切分,但这是行不通的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.