在赛普拉斯测试中将日期选择器设置为从今天开始一年
Set datepicker for one year from today in Cypress test
我正在 Cypress 中创建一个定期 运行 的验收测试,我需要它从日期选择器(React,如果重要的话)输入日期为 1 年 1 个月零 1 天创建对象(文章)的日期。例如。今天是 2020 年 4 月 22 日,我想在今天创建的文章中得到 2021 年 5 月 23 日,明天它会给出 2021 年 5 月 24 日的值等等。任何人都可以分享设置它的任何想法吗?老实说,我在谷歌上搜索了一下,但我自己也不知道,因为我对 Cypress 还很陌生,只对非常简单的事情或多或少感到自信。 :)
我猜大多数日期选择器都会有一个 input
元素,可以在其中输入日期。
假设该应用程序使用库日期选择器(因此您无需通过单击按钮来测试日期选择),您可以定位该元素并使用 Cypress .type()
命令输入未来日期。
例如,the Material UI date picker docs 有这个输入元素
<input aria-invalid="false"
id="date-picker-inline"
type="text"
class="MuiInputBase-input MuiInput-input MuiInputBase-inputAdornedEnd"
value="08/18/2014">
所以步骤应该是这样的
cy.visit('https://material-ui.com/components/pickers');
const targetDate = Cypress.moment()
.add(1, 'year')
.add(1, 'month')
.add(1, 'day')
.format('MM/DD/YYYY') // adjust format to suit the apps requirements
cy.get('input[id="date-picker-inline"]')
.clear()
.type(`${targetDate}{enter}`) // presume you need the enter key to trigger an event
此方法已弃用。你可以使用这个方法;
//click the date picker
cy.get('#datepicker').click();
//choose previous month
cy.contains('Prev').click();
//choose next month
cy.contains('Next').click();
//choose date 24
cy.contains('24').click();
我正在 Cypress 中创建一个定期 运行 的验收测试,我需要它从日期选择器(React,如果重要的话)输入日期为 1 年 1 个月零 1 天创建对象(文章)的日期。例如。今天是 2020 年 4 月 22 日,我想在今天创建的文章中得到 2021 年 5 月 23 日,明天它会给出 2021 年 5 月 24 日的值等等。任何人都可以分享设置它的任何想法吗?老实说,我在谷歌上搜索了一下,但我自己也不知道,因为我对 Cypress 还很陌生,只对非常简单的事情或多或少感到自信。 :)
我猜大多数日期选择器都会有一个 input
元素,可以在其中输入日期。
假设该应用程序使用库日期选择器(因此您无需通过单击按钮来测试日期选择),您可以定位该元素并使用 Cypress .type()
命令输入未来日期。
例如,the Material UI date picker docs 有这个输入元素
<input aria-invalid="false"
id="date-picker-inline"
type="text"
class="MuiInputBase-input MuiInput-input MuiInputBase-inputAdornedEnd"
value="08/18/2014">
所以步骤应该是这样的
cy.visit('https://material-ui.com/components/pickers');
const targetDate = Cypress.moment()
.add(1, 'year')
.add(1, 'month')
.add(1, 'day')
.format('MM/DD/YYYY') // adjust format to suit the apps requirements
cy.get('input[id="date-picker-inline"]')
.clear()
.type(`${targetDate}{enter}`) // presume you need the enter key to trigger an event
此方法已弃用。你可以使用这个方法;
//click the date picker
cy.get('#datepicker').click();
//choose previous month
cy.contains('Prev').click();
//choose next month
cy.contains('Next').click();
//choose date 24
cy.contains('24').click();