这就是我要选择的内容:
<select id="firstDate" name="firstDate" class="ab-abcd sdfg">
<option disabled="" value="">First day</option>
<option value="01.02.2019">01.02.2019</option>
<option value="01.02.2019">01.02.2019</option>
</select>
这个日期会不时更改,所以有没有办法在不使用 Cypress 的情况下选择一个选项
cy.get('#firstDate').select('01.01.2019')
?
我也试过
cy.get('#firstDate').first()
但这似乎不起作用。
最佳答案
我认为更改值的最佳方法是通过 .select()
。如果您尝试强制单击某个选项,它不会更改所选值。
是什么原因使这个值发生变化?如果它们是从服务器请求生成的,您可以尝试将其 stub like this .如果它们是根据当前日期生成的,您可以尝试 using clocks
编辑在知道是什么让选择器动态之后:
要为您的测试确定一个日期并使它们保持一致,您可以这样做
// fix the date before rendering the select
const now = new Date(2019, 01, 15).getTime() // 2019-01-02 timestamp
cy.clock(now)
cy.visit('/index.html') // visit the page to test (or make actions that render the select)
cy.get('#firstDate').select('01.02.2019') // select one month beyond
cy.get('#firstDate').select('01.03.2019') // select two months beyond
关于testing - Cypress - 选择一个选项而不使用文本或值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53709190/