英文:
Checkbox cannot be checked by cypress because it isn't of type 'checkbox'. How can I get around this?
问题
我正在尝试在行为类似复选框的元素上执行 Cypress 测试,但我收到以下错误:
cy.check() 只能在
:checkbox
和:radio
上调用
基本上,我正在测试的元素的行为类似复选框,但它是一个自定义组件。我能在这个组件上使用 .check()
等吗?
英文:
I'm trying to perform cypress tests on elements that act like checkboxes, but I'm getting the following error:
> cy.check() can only be called on :checkbox
and :radio
Basically, the element I'm testing behaves like a checkbox, but is a custom component. Can I use .check()
etc. on this component?
答案1
得分: 2
Cypress 不允许您在不是复选框或单选按钮类型的组件上使用 cy.check()
。
要对该组件进行 check
,您可能需要在组件上使用 cy.click()
。以下内容可能不是100%正确,但只需将 .check()
替换为 .click()
即可。
// 使用 .check()
cy.get('#my-custom-checkbox').check()
// 使用 .click()
cy.get('#my-custom-checkbox').click()
(这可能不准确,因为组件的可点击元素可能是您认为的复选框的子元素或同级元素 - 您可能需要进行一些实验来找到正确的要点击的元素)
英文:
Cypress won't allow you to use cy.check()
on any component that is not of type checkbox or radio.
In order to check
the component, you'll probably have to use cy.click()
on the component. The following may not be 100% correct, but it should be as simple as just swapping out .check()
for .click()
.
// Usage of .check()
cy.get('#my-custom-checkbox').check()
// Usage of .click()
cy.get('#my-custom-checkbox').click()
(This may not be accurate as the component's clickable element may be a child or sibling of what you believe is the checkbox - it may take some experimentation to find the correct element to click)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论