【发布时间】:2014-04-26 03:53:51
【问题描述】:
问题:我想知道是否可以在一个脚本文件中使用测试框架进行并行浏览,所以使用 tester 模块和 casperjs 测试命令。
我见过一些人创建了两个 casper 实例: CasperJS simultaneous requests 和 https://groups.google.com/forum/#!topic/casperjs/Scx4Cjqp7hE ,但正如文档中所说,我们无法在测试脚本中创建新的 casper 实例。
所以我尝试使用 casper 测试脚本做一些类似的简单示例(只需复制并执行它就可以了):
var url1 = "http://casperjs.readthedocs.org/en/latest/testing.html"
,url2 = "http://casperjs.readthedocs.org/en/latest/testing.html"
;
var casperActions = {
process1: function () {
casper.test.begin('\n********* First processus with our test suite : ***********\n', function suite(test) {
"use strict";
casper.start()
.thenOpen(url1,function(){
this.echo("1","INFO");
});
casper.wait(10000,function(){
casper.test.comment("If parallel, it won't be printed before comment of the second processus !");
})
.run(function() {
this.test.comment('----------------------- First processus over ------------------------\n');
test.done();
});
});
},
process2: function () {
casper.test.begin('\n********* Second processus with our test suite : ***********\n', function suite(test) {
"use strict";
casper.start()
.thenOpen(url1,function(){
this.echo("2","INFO");
});
casper.test.comment("Hi, if parallel, i'm first !");
casper.run(function() {
this.test.comment('----------------------- Second processus over ------------------------\n');
test.done();
});
});
}
};
['process1', 'process2'].forEach(function(href) {
casperActions[href]();
});
但这不是并行的,它们是一一执行的。 目前,我使用子进程进行一些并行浏览,但节点不在文件本身中。因此,如果您将我以前的代码拆分为两个文件 -proc1.js,proc2.js-(只是两个场景->casper.test.begin{...}),并通过节点启动下面的代码,类似的东西会使用 Linux,我必须搜索 windows- 的等效语法:
var exec = require("child_process").exec
;
exec('casperjs test proc1.js',function(err,stdout,stderr){
console.log('stdout: ' + stdout);
console.log('endprocess1');
});
exec('casperjs test proc2.js',function(err,stdout,stderr){
console.log('stdout: ' + stdout);
console.log('endprocess2');
});
我的问题是重定向和打开新 url 很长,所以我希望其中一些是并行执行的。我可以做 XXX 文件并与节点并行启动它们,但我不希望 XXX 文件有 5 行代码,所以如果有人成功(如果可能的话)在没有节点的同一个测试文件中并行打开 url(所以没有多个进程),请教我!
我想知道链接指令之间有什么区别,或者每次都重复使用 casper 对象:
在这之间:
casper.test.begin('\n********* First processus with our test suite : ***********\n', function suite(test) {
"use strict";
casper.start()
.thenOpen(url1,function(){
this.echo("1","INFO");
})
.wait(10000,function(){
casper.test.comment("If parallel, it won't be print before comment of the second processus !");
})
.run(function() {
this.test.comment('----------------------- First processus over ------------------------\n');
test.done();
});
});
还有:
casper.test.begin('\n********* First processus with our test suite : ***********\n', function suite(test) {
"use strict";
casper.start();
casper.thenOpen(url1,function(){
this.echo("1","INFO");
});
casper.wait(10000,function(){
casper.test.comment("If parallel, it won't be print before comment of the second processus !");
})
casper.run(function() {
this.test.comment('----------------------- First processus over ------------------------\n');
test.done();
});
});
链接我的指令,如果我的一个步骤失败(承诺被拒绝)而不是执行每个 casper 步骤,它会阻塞所有链吗?
所以最好将指令与相关步骤链接起来 [如 thenClick(selector)] 并使用带有独立步骤的 casper 对象(如打开一个新 url),不是吗?
编辑:我尝试过,如果一个步骤失败,无论是否链接,它将停止所有后续步骤,所以我看不出使用链接步骤的区别......
【问题讨论】:
-
承诺拒绝就像
throw,它将跳过链的其余部分,直到它被处理。 -
而在 casperjs 中,你知道承诺何时实现吗?什么时候完成一个步骤?因为例如,失败的测试不会阻塞链,只会阻塞。
-
我不知道 casperjs 有 promises 实现?顺便说一句,如果你想要的只是并行运行测试,那么有 grunt 和 gulp 任务可以为你做到这一点
-
"回调/监听器是 Promise 模式的一个实现。"是的,grunt 是这样做的,但我们最终还是选择手动进行。 (节点子进程)+ promises-es6.
标签: node.js parallel-processing promise functional-testing casperjs