【问题标题】:how to capture and test for socket.io events in casper.js如何在 casper.js 中捕获和测试 socket.io 事件
【发布时间】:2014-08-11 18:23:12
【问题描述】:

我正在使用 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 事件已发出,等待 500 毫秒并断言服务器已发出我希望看到的内容。这似乎是一个日常用例,但我无法让整个周期正常工作。

(请注意,在我的更新中,我发现了正在发生的错误并且它在其他地方,所以我更新以显示什么不起作用而不是错误)

【问题讨论】:

  • 请注册remote.messagepage.error 活动。也许有错误。如果您在 casper 中使用 phantomjs 作为引擎,那么它可能不起作用,因为PhantomJS doesn't support websockets。如果即使 websocket 不工作,事件仍然可以工作,您将需要发布更多代码。查看 SlimerJS 以获得替代的 CasperJS 引擎。

标签: javascript node.js sockets casperjs


【解决方案1】:

我发现了问题:

在我的 test_include.js 文件中,我错误地推入了数组。

document.emits.push(Array.prototype.slice.call(arguments));

不知道为什么我只是在没有切片的情况下推动论点,但这不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多