[英]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.