我需要保存 div 文本值并在它断言后在另一个地方?
cy.get('div').should(($div) => {
const myText = $div.innerText
})
cy.get('.value').should('have.text', myText)
最佳答案
Cypress 是异步的。这意味着如果你做这样的事情:
cy.get('div').should(($div) => {
const myText = $div.innerText
})
cy.get('.value').should('have.text', myText)
...你的测试总是会失败。 const myText = $div.innerText
将在 .should('have.text', myText)
被评估后执行。
如果你想以这种方式使用myText
,只需使用cy.get()
返回的Promise即可。 :
cy.get('div')
.then($div => {
const myText = $div.text()
// write the rest of your test here
cy.get('.value').should('have.text', myText)
})
关于javascript - 我如何保存 div 文本以便在其他地方使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56396152/