簡體   English   中英

賽普拉斯如何打破嵌套的 if 循環

[英]Cypress how to break from a nested if loop

如果滿足特定條件,我想退出嵌套的 if 循環。 以下是片段:-

  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

如果執行了 method1,我如何從 switch case 1 中脫離出來?

根據您的評論,我建議將此示例遞歸作為模板供您使用:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM