javascript – CasperJs,如何在WaitTimeout上重复一次X步?
作者:互联网
所以我想要做的是创建一个casperJS函数,它允许我们重复一个步骤X次,首先刷新页面,当这个步骤函数达到超时时.
对于由于特定页面错误/冻结而导致的不可靠测试,并减少假阴性的百分比.
我只是一个问题,我不知道如何打破这个循环,因为我在IIFE范围内,请看下面的代码:
var echoTest = function(){
casper.echo('Hi');
};
var trueFunction = function(){
return true;
};
var verifyFailedTest = function(number, trueReturn, thenFunction){
var i = 0;
//outer: <-------
for (; i <= number; i++){ // <------ how to break this loop in my function then()
//IIFE
(function(index){
if (index < number-1){
//Execute 'number-1' times the then() step (reload the page each time) if timeout or until trueReturn returns true
casper.then(function(){
casper.waitFor(function checkReturnTrue(){
return trueReturn();
}
, function then() {
thenFunction();
//break outer; break; return false; <------ here where I want to break the loop
}
, function timeout() {
casper.reload();
});
});
}
//last execution, will return the normal error if it fails each time
else if (index === number){
casper.then(function(){
casper.waitFor(function checkReturnTrue(){
return trueReturn();
}
, function then() {
console.log('else');
thenFunction();
});
});
}
else{console.log('verifyFailedTest() bug');}
})(i);
}
};
我尝试使用label,但是我遇到了语法错误.
执行:
casper.test.begin('\n*************** Suite of planned test : scenario 1 **************\n', 1, function suite(test) {
casper.start('https://www.google.fr/', function() {
verifyFailedTest(3, trueFunction, echoTest);
});
casper.run(function() {
test.done();
});
});
});
解决方法:
我认为可以在没有for循环的情况下通过将代码聚类成部分并以递归方式执行此操作:
var verifyFailedTest = function(number, repeatStep, trueReturn){
var index = 0;
function intermediate(){
casper.then(function(){
repeatStep();
casper.waitFor(function checkReturnTrue(){
return trueReturn();
}
, function then() {
this.test.pass("Test passes after " + (index+1) + " try");
}
, function timeout() {
casper.reload();
if (index < number-1) {
intermediate();
} else {
lastTry();
}
index++;
});
});
}
function lastTry(){
casper.then(function(){
repeatStep();
casper.waitFor(function checkReturnTrue(){
return trueReturn();
}
, function then() {
this.test.pass("Test passes after " + (index+1) + " try");
});
});
}
intermediate();
};
只有在尝试次数后才会出现错误.
但是如果你想使用你的IIFE,以下可能会通过重新定义thenFunction并跳过然后阻止你知道它是不必要的(doBreak === true):
var verifyFailedTest = function(number, trueReturn, thenFunction){
var i = 0, doBreak = false;
var oldThenFunction = thenFunction;
thenFunction = function(){
doBreak = true;
oldThenFunction();
};
for (; i <= number; i++){
if (doBreak) {
break;
}
// your IIFE here
(function(index){
if (index < number-1){
//Execute 'number-1' times the then() step (reload the page each time) if timeout or until trueReturn returns true
casper.then(function(){
if (doBreak) { return; }
casper.waitFor(...);
});
}
//last execution, will return the normal error if it fails each time
else if (index === number){
casper.then(function(){
if (doBreak) { return; }
casper.waitFor(...);
});
}
else{console.log('verifyFailedTest() bug');}
})(i);
}
};
标签:javascript,for-loop,closures,break,casperjs 来源: https://codeday.me/bug/20191007/1868096.html