Reputation: 1545
Scenario:
I have a data_value in field input_date as: 30/06/2020 13:40 How can I test another div contains the same value in another format - 23rd June 2020 13:40
const inp_date = cy.get('input_date').invoke('val')
cy.get('div').should('contain', inp_date)
How will I format inp_date so that it is in the format 23rd June 2020 13:40
NOTE: input_date and are in two different pages. Means, When I am in the page where I need to test the div, input will not be available
Upvotes: 1
Views: 10658
Reputation: 1789
Refer: Cypress includes moment.js
I have used Cypress.env
to transfer data between pages for validation.
Page 1:
Markup:
<input type="text" id="input_date" value="30/06/2020 13:40" />
Page 2:
Markup:
<div>23rd June 2020 13:40</div>
<div>30th June 2020 13:40</div>
Test:
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'));
Upvotes: 2