Raptor
Raptor

Reputation: 54258

Loop for casper.start only executed once

I got the following script to loop through a page's options to fetch subsequent values:

var casper = require('casper').create();
casper.on('remote.message', function (message) {
    this.echo(message);
});
casper.on( 'page.error', function (msg, trace) {
    this.echo( 'Error: ' + msg, 'ERROR' );
});
casper.start(url, function() {
    this.evaluate(function() {
        // nothing
    });
    this.then(function() {
        ddlArea_options = this.getElementsAttribute('#ddlArea option', 'value');
        for(var i = 0; i < ddlArea_options.length; i++) {
            if(ddlArea_options[i] != '') {
                this.echo(ddlArea_options[i]);
                startQuery('myID', ddlArea_options[i]);
            }
        }
    });
});

where startQuery(id, val) is a function contains casper.start():

function startQuery(id, val) {
    casper.start(url, function() {
        this.echo('startQuery started');
        var obj = {};
        obj['#' + id] = val;
        this.fillSelectors('#form1', obj, true);
        this.evaluate(function() {
            __doPostBack('ddlArea', '');
        });
        this.then(function() {
            this.echo("doPostback complete");
            var values = this.getElementsAttribute('#anotherSelect option', 'value');
            for(var i = 0; i < values.length; i++) {
                this.echo(values[i]);
            }
        });
    });
    casper.run();
}

but startQuery() is executed once only, on the last item in for-loop. What did I miss?

Upvotes: 0

Views: 80

Answers (1)

Artjom B.
Artjom B.

Reputation: 61952

You can only have one start-run pair per casper instance. start resets all the steps before, so everything that was in the queue is gone. In startQuery, you can change casper.start to casper.thenOpen and remove casper.run completely.

Upvotes: 1

Related Questions