gpt4 book ai didi

Cypress - 如何在 Cypress 中进行轮询?

转载 作者:行者123 更新时间:2023-12-02 19:07:03 25 4
gpt4 key购买 nike

在网络应用程序上,特定元素仅在页面重新加载后才可见,并且在一段时间后也可用,因此目前我已将其实现如下:

it('element-check', () => {
cy.visit('url')
// performing certain actions
cy.wait(150000)
cy.reload()
cy.contains('text').click()
})

我需要使用轮询机制,而不是固定等待 cy.wait(150000),每隔 30 秒重新加载页面并检查所需的元素,直到该元素可见。

最佳答案

您可以使用递归函数来实现此目的。

it('element-check', () => {
cy.visit('url')

let retry = 0
function isElementVisible() {

if (retry < 5 && Cypress.$('selector').length == 0) {

//Increment retry
retry++

//wait 30 seconds
cy.wait(30000)

//Reload Page
cy.reload()

//Element is not yet visible, Call the recursive function again
cy.then(isElementVisible)

} else if (retry < 5 && Cypress.$('selector').length == 1) {
cy.get('selector').click()
return

} else {
//It excedded required no. of execution
return
}
}
//Trigger the recursive function
cy.then(isElementVisible)
})

关于Cypress - 如何在 Cypress 中进行轮询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64893311/

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