CasperJS, parallel browsing WITH the testing frame

2019-02-16 21:55发布

问题:

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.

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.

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

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

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!

And i would like to know what is the difference between chaining instructions, or re-use the casper object each time :

so between that :

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

And that :

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

Chaining my instructions, will it block all the chain if one of my step fail (promise rejected) instead of executing every casper steps?

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...

回答1:

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.

To launch parallel browsing with the testing framework, you have to do multiple processes, so using node does the trick.

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.



回答2:

There's no official support for parallel browsing right now in casperjs, There are multiple work arounds I've set up several different environments and am about to test witch one is the best

I see one person is working with multiple casper instances this way.

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

Currently I am running multiple caspers in node using 'child_process'. It is very heave on both CPU and memory