简体   繁体   中英

Waiting for a page to load in CasperJS

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM