How do i clear a multi-select input using Cypress?

陌路散爱 提交于 2021-01-28 10:00:59

问题


How do i clear (deselect) all options within a multi-select input using Cypress?

The documentation here does not seem to cover this scenario: https://docs.cypress.io/api/commands/select.html#Syntax

The clear() command apparently cannot be applied to selects, as you might expect https://docs.cypress.io/api/commands/clear.html#Syntax

I have tried passing an empty string / array / null to select() but i get the following error:

CypressError: Timed out retrying: cy.select() failed because it could not find a single <option> with value or text matching: ''


回答1:


You can use cy.invoke() to call the .val('') method on the underlying JQuery element, which will cause it to be cleared.

Like this:

cy.get('select')
.invoke('val', '')

Note that this will not emit the change event. If you want to test that your code reacts to the change event, you can cause it to be emitted with cy.trigger():

cy.get('select')
.invoke('val', '')
.trigger('change')

I've also filed a feature request for this functionality, as it does seem like something that should be included with Cypress by default.



来源:https://stackoverflow.com/questions/56340978/how-do-i-clear-a-multi-select-input-using-cypress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!