Skip to content Skip to sidebar Skip to footer

Casperjs, How To Repeat A Step X Times Onwaittimeout?

So what I want to do is create a casperJS function which allows us to repeat a step X times, by refreshing the page first, when this step function reaches the timeout. For unreliab

Solution 1:

I think can do this without a for loop, by clustering your code into parts and do this recursively:

var verifyFailedTest = function(number, repeatStep, trueReturn){
    var index = 0;
    functionintermediate(){
        casper.then(function(){
            repeatStep();
            casper.waitFor(functioncheckReturnTrue(){
                    returntrueReturn();
                }
                , functionthen() {
                    this.test.pass("Test passes after " + (index+1) + " try");
                }
                , functiontimeout() { 
                    casper.reload();
                    if (index < number-1) {
                        intermediate();
                    } else {
                        lastTry();
                    }
                    index++;
                });
        });
    }
    functionlastTry(){
        casper.then(function(){
            repeatStep();
            casper.waitFor(functioncheckReturnTrue(){
                    returntrueReturn();
                }
                , functionthen() {
                    this.test.pass("Test passes after " + (index+1) + " try");
                });
        });
    }
    intermediate();
};

You'll have an error only after the number'th try.

But if you want to use your IIFE, the following might work by redefining thenFunction and skipping then block after you know that it is unnecessary (doBreak === true):

var verifyFailedTest = function(number, trueReturn, thenFunction){
    var i = 0, doBreak = false;
    var oldThenFunction = thenFunction;
    thenFunction = function(){
        doBreak = true;
        oldThenFunction();
    };
    for (; i <= number; i++){
        if (doBreak) {
            break;
        }
        // your IIFE here
        (function(index){
            if (index < number-1){
                //Execute 'number-1' times the then() step (reload the page each time) if timeout or until trueReturn returns true
                casper.then(function(){
                    if (doBreak) { return; }
                    casper.waitFor(...);
                });
            }
            //last execution, will return the normal error if it fails each timeelseif (index === number){
                casper.then(function(){
                    if (doBreak) { return; }
                    casper.waitFor(...);
                });
            }
            else{console.log('verifyFailedTest() bug');}
        })(i);
    }
};

Solution 2:

Use CasperJS's repeat(int times, Function then) function.

Post a Comment for "Casperjs, How To Repeat A Step X Times Onwaittimeout?"