0

In my test, I need to wait for a page to load before proceeding. waitForSelector and waitForText aren't working for some reason, and while I can just use a wait(value), I'd have to account for times the server might be slow and make the value a lot larger than I'd like; so I was thinking of making a while loop telling the system to wait 500 milliseconds every time it returns that 'someCSSpath' doesn't exist on the page. Is there any way to do this (maybe a "casper.DoesntExist'?), or any better way to do it?

var css3path = "body > div.container-fluid > div:nth-child(3) > div.row.ng-scope > div:nth-child(1) > a > div";
casper.waitForSelector(css3path , function(){
    this.test.assertExists(css3path ); 
    if (casper.exists(css3path ){
        this.echo("logged in!");}
    else{
        this.echo("not logged in");
    };
});

When I use a casper.wait(6000, function(){ instead of waitForSelector, it works fine.
I use waitforselector earlier with the same format, and that works too; I think it's the specific thing I'm looking for that's giving me trouble.

Also, when I use wait(6000, the test finds that CSS3 path just fine; it's just waitForSelector that can't find it.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • That's exactly what `waitForSelector()`. You probably aren't using it right. Please show your code, what you expect to happen and what actually happens (including error messages, screenshot descriptions). To be on the safe side: Please register to the `resource.error`, `page.error`, `remote.message` and `casper.page.onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf)). Maybe there are errors. – Artjom B. Jul 20 '15 at 10:03
  • Are you sure that the CSS selector exists. Maybe you're checking a desktop based CSS selector, but PhantomJS loads the mobile site that doesn't have such a selector. – Artjom B. Jul 20 '15 at 11:17
  • Which PhantomJS version do you use? In v1.x there is a bug with :nth-child selectors – Artjom B. Jul 20 '15 at 11:19
  • I use 1.9.2 I think? yup, 1.9.2. Is that what the problem is? I read somewhere that for casper I needed a version pre-2 –  Jul 20 '15 at 11:20
  • should I download a more recent version, or try to find another way to get the test to move as soon as the page is loaded? –  Jul 20 '15 at 11:34
  • You could try to use XPath selectors like this: `var x = require('casper').selectXPath; casper.waitForSelector(x("//body/div[contains(@class,'container-fluid')]/div[3]/div[contains(@class,'row') and contains(@class,'ng-scope')]/div[1]/a/div"), function(){...`; Have you printed the page source after using `wait(time)` and looked whether the element actually exists? – Artjom B. Jul 20 '15 at 12:15
  • Could I use inspect element and copy xpath in my browser, and would the code in casper look like casper.waitForSelector(x("copiedxpath"), function(){...? Also, while I haven't printed the page source specifically, the test after the wait(time) found the element in question. –  Jul 20 '15 at 12:44
  • Yes, that should work. – Artjom B. Jul 20 '15 at 13:04
  • It didn't work. It just kept on waiting, even past 20 seconds (the timeout I set). Oh well, six seconds wait isn't so bad. I'll just leave it as wait(6000). Thanks for the help. –  Jul 20 '15 at 13:36
  • https://stackoverflow.com/questions/22163583/how-to-wait-for-page-loading-when-using-casperjs/45114053#45114053 See my comment. – Puffy Jul 15 '17 at 03:55

1 Answers1

0

It would be helpful to know why exactly waitForSelector and waitForText aren't working. Are they timing out? Are they not finding the required selector or text?

If they are timing out, what I would recommend is changing the default amount of time Casper will wait for until it sends a time-out message. You change this using casper.options before you begin your tests like so:

//Set time-out to 20000 milliseconds (20 seconds)
casper.options.waitTimeout = 20000;

casper.test.begin('Begin tests', function suite(test)
{
    //Your tests go here
}

It's important to have a time-out rather than a while loop that may loop definitely. If there's something wrong with the server then the tests will time-out and you will know something is wrong.

Check out this question for more info on Javascript while loops to wait for a flag:

Javascript - wait until flag=true

Community
  • 1
  • 1
Whitehawk
  • 46
  • 1
  • 6
  • It used to time out, but for some reason it's not doing that anymore, it's just sitting there, waiting I suppose. –  Jul 20 '15 at 10:55
  • The default amount of time until time out is 5 seconds (5000 milliseconds), so that might explain why your `casper.wait(6000, function()` worked (it may take more than 5 seconds but less than 6 to load). Other than that, I'm afraid I can't offer more advice until you do what Artjom B. suggested. Any more context and/or error messages would be very helpful – Whitehawk Jul 20 '15 at 11:07
  • Out of interest, what is the entire CSS3 path that you're using? @2manysemicolons – Whitehawk Jul 20 '15 at 11:09
  • body > div.container-fluid > div:nth-child(3) > div.row.ng-scope > div:nth-child(1) > a > div –  Jul 20 '15 at 11:10
  • also, the time it takes varies from 3-6 seconds. Waitforselector never finds it. –  Jul 20 '15 at 11:11
  • This may be a longshot, but try `nth-of-type(n)`. I had a similar problem of not being able to interact with selectors that used `nth-child(n)`, even though they passed `exists` tests – Whitehawk Jul 20 '15 at 11:25
  • I'd say change it in the part of the code in your answer and see if it works first before you change large amounts of code, like I said, it's just a longshot – Whitehawk Jul 20 '15 at 11:27