如何在赛普拉斯的某个变量中存储日期

How to store date in some variable in Cypress

任何人都可以帮助我如何将字段中的日期存储到变量中。这是我正在查看的 HTML:

<input id="date" class="input_date" id="XYZ" type="date" value="2019-01-12" on_input="table()">

我试过了:

const date1 = Cypress.moment(). get('#id_value') 

如果 ID 是唯一的,您可以尝试将 val 放入变量中,如下所示。我在下面的代码中使用了 date id。

注意:在输入html tag中有two ID's,可能需要与开发团队确认这里使用哪一个

 cy.get('#date').invoke('val').then((val)=>{
   const dateValue = val;
   console.log("Here is the date:"+dateValue);
 })

尽管 Cypress 导入了 moment 库,但它没有内置允许链接的命令,但您可以添加自定义命令以使其更容易。

toMoment() 命令必须链接到先前的选择命令,如 cy.get()cy.contains()。它 returns 一个 moment 对象,然后您可以使用 invoke 来调用 moment 提供的所有方法,并进一步链接 .should() 以测试从这些方法返回的值。

例如,

规格

Cypress.Commands.add('toMoment', {prevSubject: true}, (element) => {
  return Cypress.moment(element[0].value);
});

it('input tests with moment', () => {

  cy.visit('./app/moment-with-input.html');

  cy.get('input').toMoment()
    .invoke('isValid')
    .should('eq', true);

  cy.get('input').toMoment()
    .invoke('format', 'dddd')
    .should('eq', 'Saturday');

  cy.get('input').toMoment()
    .invoke('diff', Date(2020, 2, 5), 'days')
    .should('eq', -391);

})

HTML片段(放在项目的'/app'文件夹中)

<input id="date" class="input_date" id="XYZ" type="date" value="2019-01-12" on_input="table()">