CasperJS,并行浏览与testing框架

问题:我想知道是否有可能在一个脚本文件中使用testing框架进行并行浏览,所以使用tester模块和casperjstesting命令。

我见过一些人创build了两个casper实例: CasperJS同时请求和https://groups.google.com/forum/#!topic/casperjs/Scx4Cjqp7hE ,但正如文档中所述,我们不能创build新的casper实例在一个testing脚本。

所以我试着做一个类似的简单的例子 – 一个caspertesting脚本(只需复制并执行这个工作):

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](); }); 

但不是平行的,他们是一个接一个地执行的。 目前,我做一些并行浏览,但与节点,所以不在文件本身,使用subprocess。 所以如果你把我以前的代码分成两个文件-proc1.js,proc2.js-(只是两个场景 – > casper.test.begin {…}),并通过节点启动下面的代码,类似的在Linux下工作,我必须searchwindows-的等效语法:

 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'); }); 

我的问题是,redirect和打开新的URL很长,所以我想为他们中的一些并行执行。 我可以做XXX文件,并与节点并行启动它们,但是我不想要5行代码的XXX文件,所以如果有人成功(如果可能的话)在没有节点的同一个testing文件中平行地打开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(select器)],并使用独立步骤的casper对象(如打开一个新的url),不是吗?

编辑:我试过,如果一个步骤失败,链接或不,它会停止所有的下一步,所以我没有看到使用链接步骤或不…

那么,每次链接或使用casper对象只是一个味道问题,它也是这样,我们不能在一个testing脚本中启动casper的几个实例。 如果你有一个循环打开一些链接,你将不得不等待每个页面顺序加载。

要使用testing框架来启动并行浏览,您必须执行多个进程,所以使用节点就可以做到这一点。

挖掘之后,我终于分割了太多redirect的文件,不会比我无法分割的主要场景更长。 包含15个文件的文件夹在本地机器上执行 – 并行 – 在2/4分钟。

目前在casperjs中没有官方的并行浏览支持,有很多的工作,我已经build立了几个不同的环境,即将testing一个是最好的

我看到一个人正在用这种方式处理多个casper实例。

 var google = require('casper').create(); var yahoo = require('casper').create(); google.start('http://google.com/'); yahoo.start('http://yahoo.com/', function() { this.echo(google.getTitle()); }); google.run(function() {}); yahoo.run(function() {}); setTimeout(function() { yahoo.exit(); }, 5000); 

目前我使用'child_process'在节点中运行多个caspers。 对CPU和内存都非常喜欢