Automation 如何在cypress中更改日期格式

Automation 如何在cypress中更改日期格式,automation,cypress,Automation,Cypress,情景: 我在字段输入日期中有一个数据值:30/06/2020 13:40 如何测试另一个div以另一种格式包含相同值-2020年6月23日13:40 const inp_date = cy.get('input_date').invoke('val') cy.get('div').should('contain', inp_date) 我将如何格式化inp_日期,使其格式为2020年6月23日13:40 const inp_date = cy.get('input_date').invoke(

情景:

我在字段输入日期中有一个数据值:30/06/2020 13:40 如何测试另一个div以另一种格式包含相同值-2020年6月23日13:40

const inp_date = cy.get('input_date').invoke('val')
cy.get('div').should('contain', inp_date)
我将如何格式化inp_日期,使其格式为2020年6月23日13:40

const inp_date = cy.get('input_date').invoke('val')
cy.get('div').should('contain', inp_date)
注意:输入_日期和在两个不同的页面中。意味着,当我在需要测试div的页面中时,输入将不可用

参考:

我使用了
Cypress.env
在页面之间传输数据以进行验证

第1页:
标记

<input type="text" id="input_date" value="30/06/2020 13:40" />
<div>23rd June 2020 13:40</div>
<div>30th June 2020 13:40</div>
cy.get('#input_date').then(($el) => {
    return new Cypress.Promise((resolve, reject) => {
        const formattedDate = Cypress.moment($el.val(), 'DD/MM/YYYY HH:mm').format('Do MMMM YYYY HH:mm');
        if (formattedDate) {
            resolve(formattedDate);
        } else {
            reject(0); // handle reject;
        }
    })
}).then(formattedDate => {
    Cypress.env('inputDate', formattedDate);
    cy.log('created new user on ' + Cypress.env('inputDate'));
});

cy.wait(0);
cy.log('created new user on ' + Cypress.env('inputDate'));
cy.wait(1000);
cy.log('created new user on ' + Cypress.env('inputDate'));
cy.visit('http://localhost:8081/page2');
cy.get('div').should('contain', Cypress.env('inputDate'));
测试

<input type="text" id="input_date" value="30/06/2020 13:40" />
<div>23rd June 2020 13:40</div>
<div>30th June 2020 13:40</div>
cy.get('#input_date').then(($el) => {
    return new Cypress.Promise((resolve, reject) => {
        const formattedDate = Cypress.moment($el.val(), 'DD/MM/YYYY HH:mm').format('Do MMMM YYYY HH:mm');
        if (formattedDate) {
            resolve(formattedDate);
        } else {
            reject(0); // handle reject;
        }
    })
}).then(formattedDate => {
    Cypress.env('inputDate', formattedDate);
    cy.log('created new user on ' + Cypress.env('inputDate'));
});

cy.wait(0);
cy.log('created new user on ' + Cypress.env('inputDate'));
cy.wait(1000);
cy.log('created new user on ' + Cypress.env('inputDate'));
cy.visit('http://localhost:8081/page2');
cy.get('div').should('contain', Cypress.env('inputDate'));

谢谢。但是忘了提到标签,它们在两个不同的页面中。意味着,当我在需要测试div的页面中时,输入将不可用。有什么解决办法吗?我将更新我的问题我已经用我在测试中使用的解决方案更新了我的答案;是Cypress.config吗?cy.config不是函数:)cy.get('#input_date')。然后($el=>{const formattedDate=Cypress.矩($el.val(),'DD/MM/yyyyy HH:MM')。format('Do MMMM YYYY HH:MM');cy.config('inputDate',formattedDate');cy.log('在'+Cypress.config('inputDate')上创建的新用户)//工作inputDate给我正确的值});Cypress.config('inputDate')//不在外部工作undefined@AJ:在我的测试场景中,我似乎已经从
xhr
响应中设置了配置中所需的数据。我假设
cy.get().then()
也会通过
Cypress.Promise
自动得到解析,但事实并非如此,因此我们需要显式地包装在
Cypress.Promise
中,因此数据在我的上一个回答版本中无法全局获得。我再次更新了答案,但这次我重新创建了两页的确切场景,并附上了屏幕截图。希望这次我的回答最终能有所帮助!:)我怀疑您应该使用别名而不是变量来保存
input\u date
中的日期,请参阅。要获得完整的图片,请发布整个测试,包括如何过渡到下一页。