CasperJS - How to upgrade to the next test suite?

advertisements

I'm quoting this example:

casper.test.begin('Google search retrieves 10 or more results', 5, function suite(test) {
    ...
});
casper.test.begin('Casperjs.org is first ranked', 1, function suite(test) {
    ...
});

While executing the first test suite in this example, how do I skip to the test suite? I want to jump to "Casperjs.org is first ranked" test suite in the above example?

Or in other words, is there a way to skip to the next available casper.test.begin() block?

I have tried out test.skip() and the given documentation doesn't give me ideas on how to achieve the above.


This is not possible with the public API that is provided by CasperJS.

Having said that, CasperJS' tester module manages the test cases in a queue. So you can simply call

test.queue.shift();

to remove the next test.begin() block or

test.queue.splice(1, 1); // second block after the next one
// position ----- ^  ^
// amount of blocks -ยด

to remove any future block. Note that this won't work for if the test cases themselves are not asynchronous. That is also why I used a casper.start().then().run() combo in the proof of concept.

Warning: This is not documented and may change in future versions.

Proof-of-concept:

casper.test.begin("test 1", function(test){
    casper.start().then(function(){
        test.assertTrue(true);
    }).run(function(){
        test.done();
    });
});

casper.test.begin("test 2", function(test){
    casper.start().then(function(){
        //test.queue.splice(0, 1);
        test.queue.shift();
        test.assertTrue(true);
    }).run(function(){
        test.done();
    });
});

casper.test.begin("test 3 (invisible)", function(test){
    casper.start().then(function(){
        test.assertTrue(true);
    }).run(function(){
        test.done();
    });
});

casper.test.begin("test 4 (visible)", function(test){
    casper.start().then(function(){
        test.assertTrue(true);
    }).run(function(){
        test.done();
    });
});

Output:

Test file: skip_begin_block.js
# test 1
PASS test 1 (NaN test)
PASS Subject is strictly true
# test 2
PASS test 2 (NaN test)
PASS Subject is strictly true
# test 4 (visible)
PASS test 4 (visible) (NaN test)
PASS Subject is strictly true
PASS 3 tests executed in 0.226s, 3 passed, 0 failed, 0 dubious, 0 skipped.