0

I have been using mocha-casperjs for a while now and I noticed that for some reason, sometimes code like:

casper.click(x("//a[normalize-space(text())='login']"));

or

casper.waitForSelector(x("//a[normalize-space(text())='login']")

don't work immediately, but it fails saying: xpath selector: //a[normalize-space(text())='login']" still did not exist 5000ms

After some attempts, it just works, but than it doesn't anymore again.

It is not an asynchronous problem, because I use code like:

if(casper.exists(x("//a[normalize-space(text())='login']"))){
  console.log('I am here');
}

to test its presence and this always returns "I am here".

Any idea ?

Lucamjj
  • 53
  • 6

1 Answers1

0

So, I think the problem I was having is related to this question. The question I have linked to, led me to further testing and to the solution. All the time my tests were failing, it was because I was actually already logged in, so x("//a[normalize-space(text())='login']") wasn't there obviously.

The fact that the below code returned "I am here", it was because I was doing in the wrong way.

if(casper.exists(x("//a[normalize-space(text())='login']"))){
  console.log('I am here');
}

Another thing I did wrongly that it might help someone and that I totally missed, is that I moved casper.start and casper.run outside describe("", function(){...}) like:

casper.start();

describe("", function(){
    it("...", function(){..})
    it("...", function(){..})
});

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

I hope this helps someone.

Thanks.

Community
  • 1
  • 1
Lucamjj
  • 53
  • 6