Ole*_*leg 0 javascript casperjs
我的casperjs测试中有一个页面有图像,我不要等到这个页面加载到下一步.我该怎么做 ?我试过这种方式
var casper = require("casper").create({
onStepTimeout: function() {
this.echo("TIMEOUT" + this.requestUrl,"RED_BAR");
// Some skip page controlling code
},
);
var timeout = ~~casper.cli.get(0);
casper.start("http://127.0.0.1/index2.php", function () {
this.echo("FIRST GOOD PAGE", "GREEN_BAR");
casper.options.stepTimeout = timeout;
});
casper.thenOpen("http://127.0.0.1/slowpage.php", function() {
this.echo("SECOND PAGE LOADED - I want to be called even have received Timeout!", "GREEN_BAR");
});
casper.then(function() {
this.echo("THEN!", "GREEN_BAR");
});
Run Code Online (Sandbox Code Playgroud)
但它只是调用onStepTimeout直到slopage.php被加载.
您可以添加request.abort();一个casper步骤来结束该步骤并继续下一步:
casper.then(function() {
request.abort();
this.echo('You will never see me');
});
casper.then(function() {
this.echo('I execute next');
});
Run Code Online (Sandbox Code Playgroud)
您还可以根据open要求检查是否要中止.此函数将检查匹配的URL,然后在打开之前中止,并将返回http状态代码:
casper.on('page.resource.requested', function(requestData, request) {
if (requestData.url.indexOf('slowpage.php') !== -1) {
request.abort();
}
});
Run Code Online (Sandbox Code Playgroud)
您可以更改waitTimeout和stepTimeout设置.另外,在pageSettings你可以使casper不加载图像.例:
var casper = require('casper').create ({
waitTimeout: 10000,
stepTimeout: 10000,
verbose: true,
viewportSize: {
width: 1400,
height: 768
},
pageSettings: {
"userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
"loadImages": false,
"loadPlugins": false,
"webSecurityEnabled": false,
"ignoreSslErrors": true
},
onWaitTimeout: function() {
//throw new Error
},
onStepTimeout: function() {
//throw new Error
}
});
Run Code Online (Sandbox Code Playgroud)
您可以使用casper waitFor等待页面完全加载.只是return true.它甚至有自己的timeout功能.所以你可以这样做:
casper.waitFor(function check() {
casper.thenOpen("http://127.0.0.1/slowpage.php", function() {
//+++ casper will wait until this returns true to move forward.
//+++ The default timeout is set to 5000ms
this.evaluate(function() {
//checks for element exist
if (document.getElementById('someElement')) {
console.log('Im loaded!');
return true;
}
});
});
}, function then() { // step to execute when function check() is ok
//+++ executes ONLY after the 'casper.thenOpen' returns true.
this.echo("THEN!", "GREEN_BAR");
}, function timeout() { // step to execute if check has failed
//+++ code for on timeout. This is different than onStepTimeOut.
},1000);// custom timeOut setting.
Run Code Online (Sandbox Code Playgroud)
实际上,在某些情况下,stepTimeout由于连接问题,CasperJS的步骤偶尔会因超时(如果在设置中指定)而到期.默认行为是停止CasperJS this.die.如果要求不停止CasperJS,而是继续执行后续步骤,则应提供自定义onStepTimeout.不幸的是,request.abort在这种情况下没有定义,因为request只能在onResourceRequested处理程序内部访问(这就是为什么@Topher Ellis的答案有问题).换句话说,request.abort有助于防止发出新请求,但无法阻止正在进行的请求(并且可能会超时).对于这种情况,我使用以下代码:
var casper = require("casper").create(
{
...
onStepTimeout: function(timeout, step)
{
this.echo('step timeout');
this.clear();
this.page.stop();
}
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.