问题描述
我想测试两个用户之间的交互,并使用CasperJS通过远程服务器进行通信。我的应用程序不是聊天应用程序,但这是说明我要做什么的简单方法。
I want to test the interaction between two users, communicating through a remote server, using CasperJS. My app is not a chat app, but that is an easy way to illustrate what I want to do.
因此,我将登录浏览器窗口A,然后登录浏览器窗口B,然后返回浏览器窗口A,我将输入聊天消息,在发送按钮上调用 click()
,然后返回浏览器B,我将等待出现的消息。然后写一条消息,然后返回浏览器A以确保它到达。
So I'll login browser window A, then login to browser window B, then back in browser window A I'd input the chat message, call click()
on the send button, and then back in browser B I'd wait for the message to appear. Then write a message, and go back to browser A to make sure it arrives.
我在,事实证明它是串行的。串行对我来说很好,但是似乎在每个浏览器中执行多个操作会变得非常混乱。就像这样:
I found this discussion on parallel browsing, which turns out to be serial. Serial is fine for me, but it appears doing more than one action in each browser is going to get very messy. It'd be something like this:
A.start(...);
A.then(...);
A.then(...);
B.start(...);
B.when(...);
A.run(function(){
B.run(function(){
A.start(...);
A.then(...);
A.run(function(){
B.start(...);
B.run(function(){
//and so on
});
});
});
});
(我尚未实际测试过该方法;我开始以这种方式编写,并认为必须更好的方法?!)
(I've not actually tested that will work; I started writing it that way and thought there must be a better way?!)
推荐答案
+1
我会这样做这样:
两个脚本:
- 具有A登录名的脚本A
- 使用B登录的脚本B
然后执行脚本第一步(登录后):在聊天。
脚本B的第一步:等待A文本,然后发送其答案。
脚本的第二步:等待B答案等...
Then script A first step (after login) : writing in the chat.Script B first step : waiting for A text then sending its answer.Script A second step : waiting for B answer etc...
您使用节点(子进程)并行启动这两个脚本,它们将与 wait()语句
。
You launch these two scripts in parallel using node (child process) and they will interact with the wait() statements
.
只有一个微妙的地方:等待呈现两个页面-或同时登录(或大约同时登录),因为如果其中一个冻结了一段时间,您可能会收到timeouterror ...因此,可能会增加 waitTimeout
;更安全。 (尽管对我来说5秒的默认超时就足够了。)
There is just one delicate point : wait for the two pages to be rendered -or to login- at the same time (approximatively), because if one of them freeze a little, you could get the timeouterror... So maybe increase the waitTimeout
; to be safer. (though for me the 5sec default timeout should be sufficient).
您还可以使用外部文件来同步它,但是我不知道它怎么可能会有所帮助,因为无论如何您都必须等待该文件中的数据被更新。
You could also use an external file to 'synchronize' it, but I don't see how it could be helpful, because you would have to wait for the data to be updated in this file anyway.
因此,该解决方案是异步的,但它可以工作。
So this solution it's asynchronous, but it works.
这篇关于如何测试两个交互的浏览器(例如聊天应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!