testing - Cypress - 选择一个选项而不使用文本或值

标签 testing automation cypress

这就是我要选择的内容:

<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/

相关文章:

visual-studio - Visual Studio 解决方案文件中的多个测试程序集

javascript - 使用 PhantomJS 在 Angular.js 应用程序中填写表单

excel - 运行时错误 '9' vba 代码;在不知道名称的情况下打开 xlsx 文件并对其进行更改

reactjs - Cypress session 重定向到空白页

c# - 适当的断言消息

testing - JavaFX:如何使用 Junit 和 netbeans 测试简单的 JavaFX 应用程序

c++ - Excel 自动化。如何复制多个单元格?

testing - 正则表达式在结果中只需要两个空格

selenium - 使用 Selenium 或 Cypress 进行测试时如何触发 slate.js 上的更改事件

testing - 使用 Cypress 进行 E2E NextJS 测试时如何使用测试数据库?