[英]How to get all elements until there isn't any element in cypress
A 在我的 cypress 项目中有一个命令,该命令应该从用户帐户中删除某个实体(文件)的所有注册表,并在每次测试之前运行。
它的主要步骤如下运行:
// Await loading
cy.contains('Carregando...').should('not.be.visible')
// Find all delete buttons by its svg path
cy.get(
`[d='M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z']`,
)
.parent()
.parent()
.parent()
.each((delete_button, index, list) => {
cy.wrap(delete_button).click({ force: true })
// Fills in modal using the correct deletion phrase.
cy.contains(/^Escreva "(.*)" para confirmar/).then(el => {
const reg = RegExp(/^Escreva "(.*)" para confirmar/)
const confirmation = reg.exec(el[0].innerText)[1]
cy.get(`[placeholder='${confirmation}']`).type(`${confirmation}{enter}`)
})
cy.contains('Carregando...', { timeout: 60000 }).should('not.be.visible')
})
// Asserts that table section is all clear
cy.get('span').should(
'contains.text',
'Ao inserir uma tabela no sistema você poderá criar fluxos e treinar seus modelos a partir da sua base de dados.',
)
在第一次测试中,由于项目有一些条目,因此该命令正常通过,但在接下来的测试中它将失败,因为 get 命令没有找到任何 svg 作为参考来找到屏幕上的按钮。
如何更改此命令,以便在以下两种情况下都可以传递:
看起来您需要先检查元素是否存在?
cy.get('body').then(($body) => {
if ($body.find('[d=...]').length) {
// svg was found, find the buttons
cy.get('[d=...]')
...the rest of your code...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.