在Web应用程序上,特定元素仅在页面重新加载后才可见,并且一段时间后也可用,因此目前我已如下实现:
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')
function isElementVisible() {
let retry = 0
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)
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句