peixotorms
peixotorms

Reputation: 1283

Clicking to next page on casperjs not working

I am trying to take a screenshot of the second page on a website that uses ajax, using casperjs. I used Chrome and Ressurectio to create a test script and changed it slightly in order to suit my needs.

However, when I run the script, the second screenshot only shows the "loading" page, which at first I thought it's due to ajax being slow...

The problem is that even with a timeout of 15 seconds, it still doesn't take the screenshot that I want.

Maybe I'm forgetting something?

Here's my script:

var x = require('casper').selectXPath;
casper.options.viewportSize = {width: 1366, height: 667};
casper.on('page.error', function(msg, trace) {
   this.echo('Error: ' + msg, 'ERROR');
   for(var i=0; i<trace.length; i++) {
       var step = trace[i];
       this.echo('   ' + step.file + ' (line ' + step.line + ')', 'ERROR');
   }
});
casper.test.begin('Resurrectio test', function(test) {
   casper.start('http://recrutamento.auchan.pt/listaofertas.aspx');

   casper.waitForSelector(x("//a[normalize-space(text())='>>>']"),
       function success() {
            this.capture('click1.png')
            test.assertExists(x("//a[normalize-space(text())='>>>']"));
            this.click(x("//a[normalize-space(text())='>>>']"));
            this.wait(7000);
            this.capture('click2.png')
       },
       function fail() {
           test.assertExists(x("//a[normalize-space(text())='>>>']"));
   });

   casper.run(function() {test.done();});
});

Upvotes: 0

Views: 142

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

wait is an asynchronous step function such as then, so you have to put capture into the callback of wait:

this.wait(7000, function(){
    this.capture('click2.png')
});

You're taking the screenshot to early.

Upvotes: 1

Related Questions