简体   繁体   English

等待页面加载到CasperJS中

[英]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; 由于某些原因, waitForSelectorwaitForText无法正常工作,尽管我只能使用wait(value) ,但我不得不考虑服务器可能变慢的时间,并使该值比我想要的大得多。 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. 所以我想做一个while循环,告诉系统每次返回“ someCSSpath”在页面上不存在时要等待500毫秒。 Is there any way to do this (maybe a "casper.DoesntExist'?), or any better way to do it? 有什么方法可以做到这一点(也许是“ casper.DoesntExist”吗?),或者有更好的方法吗?

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. 当我使用casper.wait(6000, function(){而不是waitForSelector ,它工作正常。
I use waitforselector earlier with the same format, and that works too; 之前我使用相同格式的waitforselector,它也可以工作; 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. 另外,当我使用wait(6000,测试发现CSS3路径很好;只是waitForSelector找不到它。

It would be helpful to know why exactly waitForSelector and waitForText aren't working. 知道为什么waitForSelectorwaitForText完全waitForSelector将很有帮助。 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. 如果他们超时,我建议更改Casper将等待的默认时间,直到它发送超时消息为止。 You change this using casper.options before you begin your tests like so: casper.options在开始测试之前使用casper.options更改此casper.options ,如下所示:

//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. 重要的是要有一个超时而不是一个肯定会循环的while循环。 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: 请查看此问题以获取有关Java的更多信息,而while循环等待标志:

Javascript - wait until flag=true Javascript-等到flag = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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