0

Im using phantomjs and casperjs to test out my website.

In my JavaScript I have a for loop using var i, and another var rando inside the loop which increments.

I am calling

console.log(i); 
console.log(rando); 

for testing, but it does not print the correct value to the command line. However, I know that the variable is changing, as the code does as intended, for example the for loop is incrementing.

I tried using

console.log("%d", i);

but no luck.

My Code using a suggested solution still no luck:

for (i=0; i<100000; i++) {  //// first loop
casper.wait(13000, function () {
    casper.then(function () {
        console.log(String.valueOf(i));
        console.log(String.valueOf(rando));
        casper.click(x(('/html/body/div[2]/div/div[2]/div/article/div[2]/section[2]/a/span')));
        console.log(timeStamp());     
    });
});
casper.then(function () {
    casper.click(x(('/html/body/div[2]/div/div[1]/div/div/a[2]')));        
});
if (rando == 14) {
    casper.then(function () {
        casper.click(x(('/html/body/div[2]/div/div[2]/div/article/header/span/button')));
        console.log(timeStamp());        
    });
    rando = 0;
} else {
    rando++;
}

}

The result is printing i as 100000 and rando as 10 every time, even when they are incremented.

Benjamin
  • 1
  • 2
  • 1
    You need to add the code in your question –  Jun 27 '16 at 13:08
  • apologies @Advancid – Benjamin Jun 27 '16 at 14:07
  • `casper.then` is asynchronous, so [this question](http://stackoverflow.com/q/750486/1816580) applies. I've closed the question as duplicate of a specific CasperJS question in order to let you understand the issue more clearly. – Artjom B. Jun 27 '16 at 14:27

2 Answers2

0

Try to use

console.log(String.valueOf(i))

If didn't work please show your code

0

You are probably not handling the promises that the methods to get values from the DOM return.

"Crawling" frameworks massively use promises, to retrieve values from the DOM, because most of the times those values are changed dynamically and are not available as soon as the DOM is retrieved to the client.

more info about Promises here

This is an old gist i made using webdriverjs wich is similar to casper/phantom: link to gist. I specifically made this gist to pratice promise handling.

Tiago Bértolo
  • 3,874
  • 3
  • 35
  • 53