【问题标题】:Cypress: I need to exit the for loop if the condition is satisfied赛普拉斯:如果条件满足,我需要退出 for 循环
【发布时间】:2021-09-09 06:40:24
【问题描述】:

下面是我正在使用的代码。

我正在使用 Cypress + Cucumber + Typescript

场景:我需要使用 for 循环获取唯一值列表。然后我将此值传递给 API 以验证某些条件,如果满足条件,我想退出循环。

为了退出循环,我在某处阅读了一个解决方案,如果我在 if 条件中使用“return false”作为第一行,那么循环将退出,这似乎工作正常。

这里的问题是,当我尝试从 for-if 循环内部为实例变量设置一个标志时,if 条件读取的值(用于退出循环)没有选择实例变量的更新值。并且循环继续运行。

下面是sn-p的代码:

class test {
  static isVinavailable: boolean = false;
  static setEligibleVehicleVinTest() {
    cy.xpath(eligibleForSaleVehicleVin).then((esv) => {
      const listingCount = Cypress.$(esv).length;
      for (let i = 0; i < listingCount; i++) {
        let text123 = esv.eq(i).text();
        genericAction.getAuthenticationKey();
        cy.fixture("authResp.json")
          .then((authResp) => {
            cy.request({
              method: "GET",
              url: vehicleCheckEligibility + text123,
              headers: {
                Authorization: authResp.access_token,
              },
            });
          })
          .then((response: any) => {
            cy.wait(5000);
            let responseDataelig = response.body;
            if (
              (responseDataelig.val1 =
                "Y" &&
                responseDataelig.val2 === "N" &&
                responseDataelig.val3 === "N")
            ) {
              this.isVinavailable = true;
            }
          });
        if (this.isVinavailable) {
          return false;
        }
      }
    });
  }
}

【问题讨论】:

    标签: cypress


    【解决方案1】:
    class test {
      static isVinavailable = false;
      static setEligibleVehicleVinTest(): Cypress.Chainable<boolean> {
        return cy.xpath(eligibleForSaleVehicleVin).each(($el) => {
          let text123 = $el.text();
          cy.fixture('authResp.json')
            .then((authResp) => {
              return cy.request({
                // your code block
              });
            })
            .then((response: any) => {
              // your code block
              if (condition) {
                this.isVinavailable = true;
                return false;
              };
            });
        }).then(() => {
          return this.isVinavailable;
        });
      }
    }
    

    【讨论】:

    • @celoagae 感谢您的回复。我在这里面临的问题是我正在点击一个 API,然后将 IF 条件添加到响应中。由于在我的代码中使用了多个 then() ,导致问题是 exit f for 循环。我相信我在链接代码时做错了。我已按照建议更改了代码。请求您帮助我链接代码,以便您提供的解决方案可以正常工作。提前感谢`````````
    • 那么我建议:1)你最好使用.each(),因为return false会破坏.each()循环。 2) 通过使用.each(),您无需定义flag。您可以直接在.each() 中打破循环。请看这里,这里和你的问题一样:github.com/cypress-io/cypress/issues/…
    • 你建议的代码工作得很好,但我的用例不同,就像我的代码中的 IF 条件在里面然后当我添加返回 false 时它不会退出循环。在编写此代码时需要您的帮助,以便它可以正常工作并在满足条件时退出循环
    • 示例代码:`````` static sampleTest() { let flagToReturn = false; cy.xpath(eligibleForSaleVehicleVin).each(($esv, index, $list)=> { cy.fixture("authResp.json") .then((authResp) => { //--这里有一些代码--- }) }).then((response: any) => { let responseDataelig = response.body; if (responseDataelig.val1=== "Y" && responseDataelig.val2=== "N" && responseDataelig.val3=== "N" ) { flagToReturn=true; return false; } }) } ```````
    • 您在评论中的示例代码与您的实际问题不同。在您的示例代码中,一旦您的 .each() 循环完成,那么您将您的标志设置为 true (或其他)。因此,您不能按照示例代码中断循环,因此您无法实现您想要的。但是在原始问题的第一个代码示例中:您错过了返回响应:尝试使用上面的return.each() 编辑我的答案。希望它会工作。
    猜你喜欢
    • 2021-08-22
    • 2022-12-19
    • 2019-02-12
    • 2021-03-24
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多