【发布时间】:2019-07-29 12:27:46
【问题描述】:
有人知道如何从 cypress 测试中的 react-select 下拉列表中选择一个选项吗?
我尝试了很多东西,但都无济于事。
似乎 react-select 使用了隐藏的输入。那柏树不能写进去。而且cypress也不能写入的div。
我不知道如何检查开发工具中的实际下拉列表也无济于事,因为它没有保持打开状态。
我正在使用:
- 反应选择 v2.4.1
- 柏树 v3.1.5
编辑 1:
@bkucera 的回答有效。我最终得到的工作代码是:
it('updates Person', () => {
cy.get('[data-id=bearbeiter]')
.find('.css-10nd86i')
.click()
.find('input')
.eq(1)
.focus()
cy.contains('Test Tester').click({ force: true })
})
我不得不在find 之后添加一个.eq(1),因为似乎有两个输入。
编辑 2:
上述解决方案最终点击了我网站上导航树中恰好包含相同文本的元素。所以没有雪茄:-(
编辑 3:
我也试过这个解决方案:
Cypress.Commands.add('setSelectOption', ({ selector, option, value }) => {
cy.get(selector)
.find('.css-10nd86i input')
.eq(1)
.focus()
.type(value, { force: true })
})
...但即使使用了force: true,我也会收到此错误:
The element typed into was:
> <input name="aeId" type="hidden" value="862333db-31cf-444c-b8ea-021c640c7a44">
Cypress considers the 'body', 'textarea', any 'element' with a 'tabindex' or 'contenteditable' attribute, or any 'input' with a 'type' attribute of 'text', 'password', 'email', 'number', 'date', 'week', 'month', 'time', 'datetime', 'datetime-local', 'search', 'url', or 'tel' to be valid typeable elements.
编辑 4:
到目前为止效果最好:
Cypress.Commands.add('setSelectOption', ({ selector, option, value }) => {
cy.get(selector)
.find('.css-10nd86i input:text')
.focus()
.type(option, { force: true, delay: 600, timeout: 330000 })
.type('{enter}', { force: true })
cy.get(selector)
.find('.css-10nd86i')
.find('input')
.eq(1)
.should('have.value', value)
})
至少它适用于短名单。文字输入缓慢。对于我们的物种列表(7000 长),我添加了 delay 和 timeout 选项。延迟似乎有所帮助,但我无法准确理解这些选项如何影响行为。有时柏树会超时:-(
编辑 5:
同时(react-select v3.0.4,cypress v3.3.2)所有测试都失败了,因为:
Expected to find element '.css-10nd86i' but never found it
我想班级已经改变了。这样一个脆弱的解决方案并不奇怪:-(
【问题讨论】:
-
你应该写你的柏树版本是什么
-
什么版本的 react-select
标签: cypress react-select