[英]Cypress how to break from a nested if loop
I want to come out of a nested if loop provided a particular condition is met.如果满足特定条件,我想退出嵌套的 if 循环。 Below is the snippet:-
以下是片段:-
cy.get(A).then(()=>{
do{
cy.get(B).each(()=>{
switch(C){
case "1":
if(some condition){
this.method1()
return false //THIS IS NOT BREAKING THE OUTERMOST THEN LOOP !!!!!
}
method2()
break
case "2":
if(some condition){
this.method3()
return false
}
method4()
break
} //switch ends
If(method1 is executed){
Flag= true
return False // THIS IS NOT Breaking the for each here
}
}) //each ends
If(flag==true){
Cy.get().click()
}
}while(someCondition) //do while ends
}) //Then ends
How do I break out from the switch case 1 if method1 is executed??如果执行了 method1,我如何从 switch case 1 中脱离出来?
Based on your comments I suggest this sample recursion as template for you to use:根据您的评论,我建议将此示例递归作为模板供您使用:
cy.get(A).then((someCondition) => {
cy.get(B).then(elements => {
conditionalRecursion()
function conditionalRecursion(index = 0) {
if (index >= elements.length) {
return
}
if(someCondition) {
switch(C){
case "1":
this.method1()
Cy.get().click()
break
case "2":
this.method3()
break
//Switch cases are supposed to have default case here too
}
return
}
switch(C){
case "1":
method2()
break
case "2":
method4()
break
//Switch cases are supposed to have default case here too
}
index++
return conditionalRecursion(index)
}
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.