如何使用赛普拉斯清除多选输入? [英] How do i clear a multi-select input using Cypress?
问题描述
如何使用赛普拉斯清除(取消选择)多选输入中的所有选项?
How do i clear (deselect) all options within a multi-select input using Cypress?
此处的文档似乎并未涵盖这种情况: https://docs.cypress.io/api/commands/select.html#语法
The documentation here does not seem to cover this scenario: https://docs.cypress.io/api/commands/select.html#Syntax
clear()
命令显然无法应用于选择,正如您所期望的那样
The clear()
command apparently cannot be applied to selects, as you might expect
https://docs.cypress.io/api/commands/clear.html#Syntax
我尝试将空字符串/数组/null传递给 select()
,但出现以下错误:
I have tried passing an empty string / array / null to select()
but i get the following error:
CypressError:超时重试:cy.select()失败,因为它找不到单个< option>.值或文本匹配:"
推荐答案
您可以使用 cy.invoke()
调用 .val('')
方法,这将导致将其清除.
You can use cy.invoke()
to call the .val('')
method on the underlying JQuery element, which will cause it to be cleared.
赞:
cy.get('select')
.invoke('val', '')
请注意,这不会发出 change
事件.如果要测试您的代码对 change
事件有反应,则可以使用
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.
这篇关于如何使用赛普拉斯清除多选输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!