【问题标题】:CasperJS, parallel browsing WITH the testing frameworkCasperJS,使用测试框架进行并行浏览
【发布时间】:2014-04-26 03:53:51
【问题描述】:

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

我见过一些人创建了两个 casper 实例: CasperJS simultaneous requestshttps://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


【解决方案1】:

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

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

经过挖掘,我终于拆分了太多重定向的文件,以至于不能比我无法拆分的主要场景长。一个包含 15 个文件的文件夹在 2/4 分钟内并行执行,在本地机器上。

【讨论】:

  • 关于使用 nodeJS 启动 casperJS 的多进程的任何详细信息?你不是面临并行化 phantomJS 的内存泄漏问题吗?它将详细说明原始问题的答案。非常感谢
  • 迟到总比没有好:不,我没有遇到并行化 phantomJS 的内存泄漏问题,我们使用了 promises(promises-es6 模块)和自定义节点模块来设置 casper 进程的最大值一次(取决于服务器容量)。所以当我们执行 75 次测试时;例如,有 15 个场景同时运行,其他场景在队列中等待前一个场景释放位置。
  • 看起来很棒,很乐意看到要点。但是如果我理解正确的话,你仍然需要为每次测试自己写下文件。将一串js而不是文件名传递给casper会很棒
  • 我不确定我是否理解您所说的,如果我回答:“我们使用 json 配置文件来设置要执行的文件夹/测试,以轻松配置 jenkins 构建“,我回答你的想法吗?这样,它使用fs.readdir 查找每个测试文件。
  • 因此您可以将测试文件放在一个文件夹中,它们会被自动找到并测试。如果您手动定义了写入测试文件的测试套件,那就太好了,但我需要的是执行动态生成的 casper 文件(这主要用于抓取,并带有一些页面交互)。由于“测试”文件的参数是在每次抓取请求时生成的,我需要将它们写到一个临时文件中,将其发送到 casper,然后销毁它。这就是为什么我会喜欢一种将 js 脚本发送到 casper 而不必将它们写入 FS 的方法(就像我们可以直接使用 phantomJS 一样)
【解决方案2】:

目前在 casperjs 中没有对并行浏览的官方支持,有多种解决方法我已经设置了几个不同的环境,并且即将测试女巫一个是最好的

我看到有人以这种方式处理多个 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' 在节点中运行多个 casper。它对 CPU 和内存都很沉重

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多