[英]CasperJS skip step on timeout
I have one page in my casperjs test that has images , I dont what to wait until this page loaded to go to the next step. 我的casperjs测试中有一个包含图片的页面,我什么也没要等到加载此页面才能进行下一步。 How can I do it ? 我该怎么做 ? I have tryed this way 我已经尝试过这种方式
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");
});
But it casper just calls onStepTimeout until slopage.php is loaded. 但是,casper只是调用onStepTimeout直到加载slopage.php。
You can add request.abort();
您可以添加request.abort();
in a casper step to end the step and proceed to the next step: 在Casper步骤中结束该步骤并继续进行下一步:
casper.then(function() {
request.abort();
this.echo('You will never see me');
});
casper.then(function() {
this.echo('I execute next');
});
You can also check if you want to abort based on open
request. 您还可以根据open
请求检查是否要中止。 This function will check the url for a match and then abort before opening and will return an http status code: 此函数将检查url是否匹配,然后在打开之前中止并返回http状态代码:
casper.on('page.resource.requested', function(requestData, request) {
if (requestData.url.indexOf('slowpage.php') !== -1) {
request.abort();
}
});
You can change the waitTimeout
and stepTimeout
settings. 您可以更改waitTimeout
和stepTimeout
设置。 Also, under pageSettings
you can make casper not load images. 另外,在pageSettings
下,您可以使pageSettings
不加载图像。 Example: 例:
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
}
});
You can use casper waitFor to wait until the page has completely loaded. 您可以使用casper waitFor等待页面完全加载。 Just return true
. 只要return true
。 It even has its own timeout
function. 它甚至具有自己的timeout
功能。 So you could do something like this: 因此,您可以执行以下操作:
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.
Indeed, in some cases a CasperJS's step can occasionally expire by timeout (if stepTimeout
is specified in the settings) due to problems with connection. 确实,在某些情况下,由于连接问题,CasperJS的步骤有时会因超时而终止(如果在设置中指定了stepTimeout
)。 Default behaviour is to stop CasperJS by this.die
. 默认行为是通过this.die
停止this.die
。 If it's required to not stop CasperJS, but rather continue executing next steps, one should provide a custom onStepTimeout
. 如果需要不停止CasperJS,而是继续执行下一步,则应该提供一个自定义onStepTimeout
。 Unfortunately, request.abort
is not defined in this context, because request
is only accessible inside onResourceRequested
handler (this is why the answer of @Topher Ellis is questionable). 不幸的是,在此上下文中未定义request.abort
,因为只能在onResourceRequested
处理程序中访问request
(这就是@Topher Ellis的答案值得怀疑的原因)。 In other words, request.abort
is useful to prevent a new request from being issued, but can not stop a request which is already in progress (and can be potentially timed out). 换句话说, request.abort
有助于防止发出新请求,但不能停止已经在进行中(可能会超时)的请求。 For such situations I'm using the following code: 对于这种情况,我使用以下代码:
var casper = require("casper").create(
{
...
onStepTimeout: function(timeout, step)
{
this.echo('step timeout');
this.clear();
this.page.stop();
}
});
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.