如何检查复选框是否处于不确定状态(既未选中也未选中)?
How can you check whether a checkbox is in the indeterminate state (neither checked nor unchecked)?
在 Cypress 中,您可以通过以下方式测试复选框是否被选中:
cy.get('[data-cy=my-selector]').should('be.checked');
反过来你可以通过以下方式检查是否未检查:
cy.get('[data-cy=my-selector]').should('not.be.checked');
我找不到检查复选框是否处于不确定状态的方法。
在 Cypress 中有什么方法可以做到这一点吗?
您可以为此使用 Jquery 属性 .is(":indeterminate")
。考虑你的codepen例子。你可以这样做:
cy.get('input#tall').then(($ele) => {
if ($ele.is(':indeterminate')) {
//Do something here when checkbox is in indeterminate state
expect(true).to.be.true //Or, Apply a true assertion
}
})
我刚刚意识到我可以用一个简单的 css 选择器来做到这一点:
cy.get('[data-cy=my-selector]:indeterminate').should('exist');
感谢@AlapanDas 指出该选择器。
这是另一种可能更直接的方法来检查 Cypress 中的不确定属性:
cy.get('#checkbox-input')
.invoke('prop', 'indeterminate', true)
在 Cypress 中,您可以通过以下方式测试复选框是否被选中:
cy.get('[data-cy=my-selector]').should('be.checked');
反过来你可以通过以下方式检查是否未检查:
cy.get('[data-cy=my-selector]').should('not.be.checked');
我找不到检查复选框是否处于不确定状态的方法。
在 Cypress 中有什么方法可以做到这一点吗?
您可以为此使用 Jquery 属性 .is(":indeterminate")
。考虑你的codepen例子。你可以这样做:
cy.get('input#tall').then(($ele) => {
if ($ele.is(':indeterminate')) {
//Do something here when checkbox is in indeterminate state
expect(true).to.be.true //Or, Apply a true assertion
}
})
我刚刚意识到我可以用一个简单的 css 选择器来做到这一点:
cy.get('[data-cy=my-selector]:indeterminate').should('exist');
感谢@AlapanDas 指出该选择器。
这是另一种可能更直接的方法来检查 Cypress 中的不确定属性:
cy.get('#checkbox-input')
.invoke('prop', 'indeterminate', true)