如何使用 Cypress 清除多选输入?

ric*_*3po 4 cypress

如何使用赛普拉斯清除(取消选择)多选输入中的所有选项?

这里的文档似乎没有涵盖这种情况:https ://docs.cypress.io/api/commands/select.html#Syntax

clear()命令显然不能应用于选择,正如您所期望的 https://docs.cypress.io/api/commands/clear.html#Syntax

我尝试传递一个空字符串/数组/null,select()但出现以下错误:

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

Zac*_*ist 5

自 Cypress 8.6.0 起,Cypress 支持此功能:/sf/answers/4867621461/


8.6.0之前:

您可以使用cy.invoke()调用.val('')底层 JQuery 元素上的方法,这将导致它被清除。

像这样:

cy.get('select')
.invoke('val', '')
Run Code Online (Sandbox Code Playgroud)

请注意,这不会发出change事件。如果您想测试您的代码对change事件的反应,您可以使用以下命令发出它cy.trigger()

cy.get('select')
.invoke('val', '')
.trigger('change')
Run Code Online (Sandbox Code Playgroud)

我还提交了针对此功能的功能请求,因为它看起来确实应该默认包含在 Cypress 中。