有没有办法检查一个元素在 cypress.io 中是否有 class?
Is there a way to check if an element has a class in cypress.io?
我不想测试它是否具有 class 之类的 ...should('have.class', "some-class")
我只想知道它是否具有,如果没有,则执行一些操作使其具有 class.
基本上,我想看看元素是否有 Mui-checked
,如果没有,他们会以编程方式检查它。
您可以使用 hasClass() jquery 方法:
cy.get('selector').then(($ele) => {
if ($ele.hasClass('foo')) {
//Do something when you have the class
} else {
//Do something when you don't have the class
}
})
像这样添加 class。您不需要检查条件,因为 $el.addClass()
两种方式都有效。
cy.get('selector').then($el => $el.addClass("blue"))
我不想测试它是否具有 class 之类的 ...should('have.class', "some-class")
我只想知道它是否具有,如果没有,则执行一些操作使其具有 class.
基本上,我想看看元素是否有 Mui-checked
,如果没有,他们会以编程方式检查它。
您可以使用 hasClass() jquery 方法:
cy.get('selector').then(($ele) => {
if ($ele.hasClass('foo')) {
//Do something when you have the class
} else {
//Do something when you don't have the class
}
})
像这样添加 class。您不需要检查条件,因为 $el.addClass()
两种方式都有效。
cy.get('selector').then($el => $el.addClass("blue"))