如果一个按钮被禁用那么我想跳过点击它,如果它被启用我想点击使用柏树
If a button is disabled then i want to skip clicking it, and if its enabled i want to click in using cypress
我试过下面的代码,但它仍然试图点击按钮,即使它被禁用并且测试失败。
cy.get('button').should('not.be.disabled').click()<br>
cy.get('button').should('be.enabled').click()
我建议invoke attribute:
cy.get('button')
.invoke('attr', 'disabled')
.then(disabled =>{
disabled ? cy.log('buttonIsDiabled') : cy.get('button').click()
})
您可以使用 jQuery disabled-selector 来实现这一点。您的代码应如下所示:
cy.get('button').then(($btn) => {
if ($btn.is(":disabled")) {
return
} else {
cy.wrap($btn).click()
}
})
我试过下面的代码,但它仍然试图点击按钮,即使它被禁用并且测试失败。
cy.get('button').should('not.be.disabled').click()<br>
cy.get('button').should('be.enabled').click()
我建议invoke attribute:
cy.get('button')
.invoke('attr', 'disabled')
.then(disabled =>{
disabled ? cy.log('buttonIsDiabled') : cy.get('button').click()
})
您可以使用 jQuery disabled-selector 来实现这一点。您的代码应如下所示:
cy.get('button').then(($btn) => {
if ($btn.is(":disabled")) {
return
} else {
cy.wrap($btn).click()
}
})