CasperJS WaitFor 不等待条件完成
CasperJS WaitFor not waiting for the condition to finish
我对 casperJS.I 比较陌生,有我想在页面 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() function.What 我错过这里了吗?
您应该在脚本中对每个 casper
实例仅使用一次 start
和 run
函数。由于 CasperJS 的执行是异步的,所以你不能 return 某些东西。你必须等待它。
您可以使用第二个 casper
实例来设置全局变量,并在前一个实例中使用 waitFor
等待它发生变化 here.
根据您从响应中提取 URL 的方式,在页面上下文中使用 __utils__.sendAJAX(someURL)
可能更容易(通过 casper.evaluate
)。请注意,这不应在 casper.waitFor
中完成,因为它具有默认的同步模式,无需等待。此外 waitFor
的第一个函数回调经常被调用以检查值是否更改,因此不应该那样使用。
第三种可能性是仅使用一个实例进行简单导航。似乎您在解析其他页面后重新加载了原始页面。你为什么不正常地用thenOpen
访问'someURL'
,解析然后用thenOpen
打开原来的URL而不等待?您甚至可以 get the current URL 并将其保存为(半)全局变量,以确保您返回到正确的页面。
我对 casperJS.I 比较陌生,有我想在页面 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() function.What 我错过这里了吗?
您应该在脚本中对每个 casper
实例仅使用一次 start
和 run
函数。由于 CasperJS 的执行是异步的,所以你不能 return 某些东西。你必须等待它。
您可以使用第二个 casper
实例来设置全局变量,并在前一个实例中使用 waitFor
等待它发生变化 here.
根据您从响应中提取 URL 的方式,在页面上下文中使用 __utils__.sendAJAX(someURL)
可能更容易(通过 casper.evaluate
)。请注意,这不应在 casper.waitFor
中完成,因为它具有默认的同步模式,无需等待。此外 waitFor
的第一个函数回调经常被调用以检查值是否更改,因此不应该那样使用。
第三种可能性是仅使用一个实例进行简单导航。似乎您在解析其他页面后重新加载了原始页面。你为什么不正常地用thenOpen
访问'someURL'
,解析然后用thenOpen
打开原来的URL而不等待?您甚至可以 get the current URL 并将其保存为(半)全局变量,以确保您返回到正确的页面。