【问题标题】:Cypress how to break from a nested if loop赛普拉斯如何打破嵌套的 if 循环
【发布时间】:2021-08-02 22:54:15
【问题描述】:

如果满足特定条件,我想退出嵌套的 if 循环。下面是sn-p:-

  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 命令回调中嵌套 do/wihile 循环,因为它在异步环境中是同步的。还要对条件变量进行控制台日志 - 在未能停止代码的行上,因为很可能整个 If 语句没有被执行。另一件重要的事情 - DO/while 循环至少执行一次,即使停止条件有效 - 所以我不建议使用它。
  • @RosenMihaylov 我希望这个循环至少执行一次。因此do while。要回答您的第一个问题——我不想抛出错误,我只想在满足我的条件时终止所有循环。您能否详细说明“在 cy 命令回调中嵌套 do/wihile 循环”
  • 如果我在 cypress 测试中需要 sycn 代码,我会使用如下链:cy.log('doWhileLoop').then(()=>{ ///Code }。此外,如果您需要存储条件变量 - 将它们存储在数组或对象或别名中,因为 cypress 不会将内容存储在 let/const 变量中。其实问题更多的是条件变量的存储,然后是嵌套客栈这种情况
  • @RosenMihaylov 我接近解决这个问题只需要弄清楚如何根据 then 子句中的条件变量重复我的 for each 多次: //Need to repeat the forEach again if flag =真。如何做到这一点? foreach(()=>{ // 这里有一些代码 }).then(()=>{ if(some condition) {var flag=true} else { flag=false} })

标签: javascript loops asynchronous conditional-statements cypress


【解决方案1】:

根据您的 cmets,我建议将此示例递归作为模板供您使用:

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)
        }
    })
})

【讨论】:

  • 谢谢@RosenMihaylov,我稍微调整了你的样本,递归对我有用。
猜你喜欢
  • 1970-01-01
  • 2022-12-19
  • 2021-02-28
  • 1970-01-01
  • 2019-02-12
  • 2022-07-21
  • 2019-04-26
  • 2012-03-30
相关资源
最近更新 更多