微调控制项中的因式分解是查看"内部"过程何时完成的好方法,您可以继续执行下一个删除操作.
但你应该等待旋转器出现,然后消失.在测试中,Cypress 会非常迅速地判断旋转器,甚至在它出现之前.
cy.get('.cart-toggle-btn').click()
cy.contains('.remove.btn', 'Remove').each($item => {
cy.wrap($item).click()
cy.get('spinner').should('be.visible') // check visible rather than exists
cy.get('spinner').should('not.be.visible')
})
使用列表计数器
如果你的旋转器有问题(并不在意),判断列表的长度
如果在删除操作之间重新呈现列表,并且得到detached from DOM个错误,请重新查询循环中的元素
cy.contains('.remove.btn', 'Remove').each($item => {
// get fresh count each time
cy.contains('.remove.btn', 'Remove').then($currentList => {
const count = $currentList.length
cy.contains('.remove.btn', 'Remove').first().click() // remove one
// wait for smaller list (when it's not yet empty)
if (count > 1) {
cy.contains('.remove.btn', 'Remove').should('have.length', count -1)
} else
cy.contains('.remove.btn', 'Remove').should('not.exist')
}
})
})
使用传统的Java脚本for-loop
这是较短的,使用常规循环并在循环外执行最后一个删除操作.
cy.get('.remove.btn').its('length').then(count => {
for (let index = 1; index < count; index++) {
cy.get('.remove.btn').first().click()
cy.get('.remove.btn').should('have.length', count -index)
}
cy.get('.remove.btn').first().click()
cy.get('.remove.btn').should('not.exist')
})