Question

I am trying to use CasperJS to automate some steps that usually require a lot of time to do. Basically I need to login to our CMS and check if some plugins are installed. If they are then just update them but if they're not then create them. I managed to login and get to the page with the list of the plugins but I got into trouble here. Here's what I need to do, in pseudocode:

for every plugin defined in my config file
    populate the search plugins form and submit it
    evaluate if the response contains my plugin

Here is the code

casper.thenOpen(snippetsUrl, function() {
    console.log(colorizer.colorize("###############PROCESSING SNIPPETS###################", 'ERROR'));
    this.capture('snippets.png');

    this.each(Config.snippets.station, function(self, snippet) {
        self.fill('form[id="changelist-search"]', {q: snippet.name}, true);
        self.then(function() {
            this.capture(snippet.name + '.png');
        })
    });
});

What happens is that my form gets submitted multiple times in a row and on my "then" step i end up capturing the same page multiple times...How to resolve this?

Was it helpful?

Solution

Try this:

this.each(Config.snippets.station, function(self, snippet)
{
    self.then(function()
    {
        this.fill('form[id="changelist-search"]', {q: snippet.name}, true);
    });
    self.then(function()
    {
        this.capture(snippet.name + '.png');
    })
});

The reason why your initial code did not work is that Capser's then declares a deferred execution step. If unwinded, your code did actually the following:

submit form 1
place capture 1 into a queue
submit form 2
place capture 2 into a queue
submit form 3
place capture 3 into a queue
// then execute the queue
capture 1
capture 2
caprure 3

The resulting code has all steps placed into the queue, so they are executed in proper order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top