问题描述
问题:我想知道是否有可能在一个脚本文件中使用测试框架进行并行浏览 ,因此可以使用tester模块和casperjs test命令.
Question : I would like to know if it's possible to do parallel browsing with the testing framework in one script file, so with the tester module and casperjs test command.
我已经看到有人创建了两个Casper实例: CasperJS同时请求和 https://groups.google.com/forum/#!topic/casperjs/Scx4Cjqp7hE ,但是如文档中所述,我们无法创建测试脚本中的新Casper实例.
I've seen some people create two casper instances :CasperJS simultaneous requests and https://groups.google.com/forum/#!topic/casperjs/Scx4Cjqp7hE , but as said in the doc, we can't create new casper instance in a test script.
所以我尝试用casper测试脚本做一些类似的简单示例(只需复制并执行它就可以了):
So i tried doing something similar-simple example- with a casper testing script (just copy and execute this it will work):
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 {...}),然后通过node启动下面的代码,则将与Linux一起使用,我必须搜索Windows-的等效语法:
But it's not parallel, they are executed one by one.Currently, i do some parallel browsing but with node so not in the file itself, using child process. So if you split my previous code in two files -proc1.js,proc2.js- (just the two scenarios->casper.test.begin{...}), and launch the code below via node, something like that will work-with Linux, i have to search the equivalent syntax for 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(因此(没有多个过程),请教我!
My problem is that the redirections and open new urls is quite long, so i want for some of them being execute in parallel. I could do XXX files and launch them in parallel with node, but i don't want XXX files with 5 lines of code, so if someone succeeded (if it's possible) to open urls in parrallel in the same testing file without node (so without multiple processes), please teach me!
我想知道链接指令之间的区别是什么,或者每次都重复使用casper对象:
And i would like to know what is the difference between chaining instructions, or re-use the casper object each time :
所以在这之间:
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步骤,它将阻塞所有链条吗?
Chaining my instructions, will it block all the chain if one of my step fail (promise rejected) instead of executing every casper steps?
因此,最好将指令与相关步骤链接在一起(例如thenClick(selector)),然后将casper对象与独立步骤一起使用(例如打开新的url),不是吗?
So it would be better to chaining instructions with dependant steps [like thenClick(selector)] and use the casper object with independant steps (like open a new url), wouldn't it?
我尝试过,如果某个步骤失败(是否已链接),它将停止所有后续步骤,因此无论是否使用链接的步骤,我都看不到区别...
Edit : i tried and if a step fail, chained or not, it will stop all the next steps, so i don't see the difference using chained steps or not...
推荐答案
好吧,每次链接,使用casper对象只是一个口味问题,它的作用相同,我们无法在其中启动多个casper实例.测试脚本.如果您有一个打开某些链接的循环,则必须等待每个页面依次加载.
Well, chaining or using the casper object each time is just a matter of taste, it does the same, and we can't launch several instances of casper in a testing script. If you have a loop which opens some links, you'll have to wait for each page to be loaded sequentially.
要使用测试框架启动并行浏览,您必须执行多个过程,因此使用node可以解决问题.
To launch parallel browsing with the testing framework, you have to do multiple processes, so using node does the trick.
在进行挖掘之后,我最终将具有太多重定向的文件分割成不超过我无法分割的主要方案的时间.在本地计算机上2/4分钟内并行执行一个包含15个文件的文件夹.
After digging, I finally split files with too many redirections to be not longer than my main scenario which can't be split. A folder with 15 files is executed -parallel- in 2/4 min, on local machine.
这篇关于CasperJS,使用测试框架进行并行浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!