gpt4 book ai didi

javascript - 如何使用 then() 并在 Cypress 中获取值

转载 作者:行者123 更新时间:2023-12-04 14:03:48 24 4
gpt4 key购买 nike

我有一个值为2的span元素。我想检查该值是否大于0,但是在网上检查并实现了每个方法后,它都不起作用......
这是我记录 $span 时的控制台
enter image description here
我知道 Cypress 是异步工作的,所以我使用 .then()获取元素的文本。如何获得 2 的值并按照 if-else 执行?
HTML

<div>
<span class="badge ml-1 badge-primary">2</span>
</div>
   cy.get(".badge.ml-1.badge-primary").then(($span)=> {
if($span.text().includes(0)) {
doFunction1()
} else {
cy.get(xxxxx)
}
)}

最佳答案

通过链接从 element 开始的转换方法来做到这一点-> text -> number

cy.get(".badge.ml-1.badge-primary")
.invoke('text') // to text
.then(text => +text) // to number
.then(value => {
if(value > 0) {
doFunction1()
} else {
cy.get(xxxxx)
}
})

等待值2
cy.get(".badge.ml-1.badge-primary")
.invoke('text') // to text
.then(text => +text) // to number
.should('eq', 2) // wait for value 2
.then(() => {
doFunction1() // now doFunction
})

关于javascript - 如何使用 then() 并在 Cypress 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69180716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com