1

I'm relatively new to casperJS.I have script where i want to perform operation in page B(i.e Verify the confirmation mail for user),then proceed with execution in page A.code snippet is as follows

casper.waitFor(function(){
   return this.run(function(){
     return verifyEmail(user_details['email']);
  });
},function then(){
   this.wait(60000, function() {
   this.reload(function(){
     this.echo("Refresh");
     this.capture('after-reload-a.png');
    });
  });   
}); 

and the function verifyEmail is defined as follows:

function verifyEmail(email){
    return casper.open('someURL').then(function(response){
        //extract URL from response
                    this.echo("URL"+url);
        casper.start().thenOpen(url, function() {
            this.waitForText('someText',function(){
                this.capture("final.jpg");
            });
        });
        return url;
    });
};

During the execution,Casper never executes the function verifyEmail(URL is never printed) and proceeds with then() function.What am i missing here?

Devi
  • 61
  • 9

1 Answers1

1

You should use start and run functions only once per casper instance in your script. Since CasperJS' execution is asynchronous, you can't return something. You have to wait for it.

You can use a second casper instance to set a global variable and use a waitFor in the previous instance to wait for it to change as seen here.

Depending on how you extract the URL from the response, it might be easier to use __utils__.sendAJAX(someURL) in the page context (through casper.evaluate). Note that this shouldn't be done in a casper.waitFor, because it has a default synchronous mode where wait is not necessary. Besides the first function callback of waitFor is called very often to check if a value changed, so it shouldn't be used that way.

The third possibility is to simply navigate using only one instance. It seems that you reload the original page after you parsed the other page. Why don't you normally visit 'someURL' with thenOpen, do your parsing and then open the original URL with thenOpen without any waiting? You can even get the current URL and save it a (semi-)global variable to make sure you come back to the correct page.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • How this can be achieved using casperJS test instance?is it possible to spawn two instance? – Devi Jan 21 '15 at 07:32
  • It isn't possible. However, maybe you can completely forget about a second instance or ajax. It seems that you reload the original page. Why don't you normally visit `'someURL'` with `thenOpen`, do your parsing and then open the original URL with `thenOpen` without any waiting? You can even get the current URL to make sure you come back to the correct page. – Artjom B. Jan 21 '15 at 07:36