问题:我想知道是否可以进行并行浏览将测试框架放在一个脚本文件中,因此使用 tester 模块和 casperjs test 命令。
我见过有些人创建了两个 casper 实例:CasperJS并发请求 and 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');
});
我的问题是重定向和打开新网址相当长,所以我希望其中一些并行执行。我可以做 XXX 文件并与节点并行启动它们,但我不想要带有 5 行代码的 XXX 文件,所以如果有人成功(如果可能的话)在没有节点的同一个测试文件中并行打开 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),不是吗?
编辑:我尝试过,如果一个步骤失败,无论是否链接,它将停止所有后续步骤,所以我看不出使用或不使用链接步骤的区别...