假设 checkBox() 函数返回 cy.get('.checkbox'),我认为
checkBox().its('checked').should('exist')
失败,因为 checkBox() 不返回仅包含属性的对象。它返回整个元素(我认为是一个数组)。所以你不能直接在 checkbox() 上使用它的('checked')。
无论如何,要做你想做的事,你可以使用几种方法,
使用 invoke('attr', 'checked')
checkBox().invoke('attr', 'checked')
.should('exist')
使用 getAttribute js 函数并期待 chai 断言
checkBox().then($el => {
expect($el[0].getAttribute('checked')).to.exist;
})
在 js 中使用属性,在 cypress 中使用 (its, wrap)。
注意:如前所述,您不能直接在 cy.get() 上使用它。您需要从对象中提取属性并使用 cy.wrap()
checkBox().then($el => {
cy.wrap($el[0].attributes)
.its('checked')
.should('exist')
})
您可以使用其中任何一种方法,但我推荐的是您的第一种方法。
干杯。希望对您有所帮助。