我有一个要求,一旦我注册用户注册,我的 ID 就会在后端生成,并且网页几乎在 30 秒到 120 秒之间显示该 ID。
所以当我使用:
cy.get('header > p').invoke('text').then('text' => {
cy.log("User Id: " + text)
})
注册完成后我可能没有它,所以我需要每隔 10 秒左右等待一次循环检查,直到用户 ID 显示为选择器“header > p”的一部分
尝试了不同的循环逻辑等,包括。等待并检查,但由于请求的异步性质,变量范围对我不起作用,因此尝试了 cy.wrap('false').as('found') 然后尝试在 cy.get(this .found),但是一旦我需要将其链接到.then。
那么有没有一种简单的方法可以使用 command.js 来存储全局变量值?我可以在其中更新
while(!variableFound) {
cy.get('header > p').invoke('text').then('text' => {
cy.log("User Id: " + text)
if(text.length > 0) {
variableFound = true
} else {
variableFound = false
}
})
}
最后从论坛尝试了以下代码,即使用户 ID 在 header>p 标记内可用,它仍然无法识别。
let found = false
let count=0
while (!found) {
const nonExistent = Cypress.$('header > p')
// this is always evaluating to 0 even when p is become available and contains has the text
cy.log("Length: " + nonExistent.length)
if (!nonExistent.length) {
cy.visit("/profile/")
found = false
count=count+1
cy.wait(5000)
cy.visit("/dashboard/")
if(count==18) {
found = true
cy.log('Element not found after 18 attempts.Exit from loop!!!')
}
} else {
found = true
cy.get('header > p').invoke('text').then((useridtext) => {
cy.log("User Id: " + useridtext)
})
}
}
真诚感谢任何建议。非常感谢。