CasperJS,与测试框架并行浏览

Fan*_*nch 8 parallel-processing functional-testing node.js promise casperjs

问题:我想知道是否可以在一个脚本文件中使用测试框架进行并行浏览,因此使用tester模块和casperjs测试命令.

我见过有些人创建了两个casper实例: CasperJS同时请求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]();
});
Run Code Online (Sandbox Code Playgroud)

但它不是平行的,它们是一个接一个地执行的.目前,我使用子进程进行一些并行浏览但是使用节点而不是文件本身.所以,如果你将我以前的代码拆分为两个文件-proc1.js,proc2.js-(只是两个scenario-> casper.test.begin {...}),并通过node启动下面的代码,类似的那样使用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');
});
Run Code Online (Sandbox Code Playgroud)

我的问题是重定向和打开新的网址很长,所以我想要一些并行执行.我可以做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();
    });
});
Run Code Online (Sandbox Code Playgroud)

然后 :

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();
    });
});
Run Code Online (Sandbox Code Playgroud)

链接我的指令,如果我的一个步骤失败(承诺被拒绝)而不是执行每个casper步骤,它会阻止所有链吗?

所以最好用依赖步骤链接指令[比如thenClick(selector)]并使用带有独立步骤的casper对象(比如打开一个新的url),不是吗?

编辑:我试过,如果一个步骤失败,链接或不链接,它将停止所有后续步骤,所以我没有看到使用链式步骤或不...

Fan*_*nch 3

好吧,每次链接或使用 casper 对象只是一个品味问题,它的作用是一样的,而且我们不能在测试脚本中启动多个 casper 实例。如果您有一个打开某些链接的循环,则必须等待每个页面按顺序加载。

要使用测试框架启动并行浏览,您必须执行多个进程,因此使用节点可以解决问题。

经过挖掘,我最终拆分了具有太多重定向的文件,使其不长于无法拆分的主要场景。在本地计算机上,一个包含 15 个文件的文件夹将在 2/4 分钟内并行执行。

  • 迟到总比不到好:不,我没有遇到并行化 phantomJS 的内存泄漏问题,我们使用了 Promise(promise-es6 模块)和自定义节点模块来设置 casper processus 的最大值(取决于服务器容量) 。所以当我们执行 75 次测试时;例如,有 15 个场景同时运行,而其他场景则在队列中等待前一个场景释放位置。 (2认同)