簡體   English   中英

如何解決在 foreach 循環內將值設置為 true 的問題,同時在 Cypress 的循環外部將值設置為 true

[英]How to solve setting a value to true inside a foreach loop while also setting the value to true on the outside of the loop in Cypress

我在嘗試解決這個問題時遇到了一些問題。 我想要發生的是,如果 forEach 循環在遍歷所有單元格后沒有找到第一個文本。 然后外部的最后一個 if 塊應該運行,並斷言第一個文本單元格沒有找到。

謝謝:-)

Cypress.Commands.add("aCommand", (Locator, firstCellText, secondCellText) => {

  let firstCellTextFound = false
  cy.get("aDifferentLocator").find(Locator).each($cell => {
    let text = $cell.text()

    if (text.includes(firstCellText)) {
      cy.wrap($cell).next().should("have.text", secondCellText)

      firstCellTextFound = true
    }
  })

  if(firstCellTextFound == false)
  {
    expect(firstCellTextFound).to.be.true
  }
})

您的外部if塊是同步代碼,而您的cy.get()命令是異步的,因此if塊將在cy.get().each()仍在運行時執行。 將 final if包裝在.then()塊中應該可以解決您的問題。

let firstCellTextFound = false
cy.get("aDifferentLocator").find(Locator).each($cell => {
  ... code ...
}).then(() => {
  if(firstCellTextFound == false) {
    expect(firstCellTextFound).to.be.true
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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