Checkbox不能被Cypress选中,因为它不是’checkbox’类型。如何解决这个问题?

huangapple go评论60阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年7月3日 21:19:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定