CasperJS等待条件无法完成

时间:2015-01-21 05:15:18

标签: javascript testing asynchronous casperjs

我对casperJS相对较新。我有脚本我想在页面B中执行操作(即验证用户的确认邮件),然后继续执行页面A.code片段如下

casper.waitFor(function(){
   return this.run(function(){
     return verifyEmail(user_details['email']);
  });
},function then(){
   this.wait(60000, function() {
   this.reload(function(){
     this.echo("Refresh");
     this.capture('after-reload-a.png');
    });
  });   
}); 

并且函数verifyEmail定义如下:

function verifyEmail(email){
    return casper.open('someURL').then(function(response){
        //extract URL from response
                    this.echo("URL"+url);
        casper.start().thenOpen(url, function() {
            this.waitForText('someText',function(){
                this.capture("final.jpg");
            });
        });
        return url;
    });
};

在执行期间,Casper从不执行函数verifyEmail(URL永远不会打印)并继续执行then()函数。我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

您应该在脚本中的每个start实例中仅使用runcasper个函数。自CasperJS'执行是异步的,你不能返回一些东西。你必须等待它。

您可以使用第二个casper实例设置全局变量,并使用前一个实例中的waitFor等待它更改为here

根据您从响应中提取网址的方式,在页面上下文中使用__utils__.sendAJAX(someURL)可能更容易(通过casper.evaluate)。请注意,这不应该在casper.waitFor中完成,因为它具有默认的同步模式,其中不需要等待。除了waitFor的第一个函数回调之外,还经常调用以检查值是否已更改,因此不应该以这种方式使用它。

第三种可能性是仅使用一个实例进行导航。解析另一页后,您似乎重新加载了原始页面。为什么通常不使用'someURL'访问thenOpen,进行解析,然后使用thenOpen打开原始网址,无需等待?你甚至可以get the current URL并保存一个(半)全局变量,以确保你回到正确的页面。