Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two date values in cypress

I'm trying to check if one date value that I get from the element in the app is less than today's date:

 const todaysDate = Cypress.moment().format('DD/MM/YYYY')

  it("Check date to be less or equal than todays", () => {
      cy.get('.date', { timeout: 15000 }).eq(3).invoke('text').should('be.lte', todaysDate);
    })

However I'm getting the following error:

Timed out retrying after 4000ms: expected '12/14/2020' to be a number or a date

Is there a way to convert the date I get from element to a datetime object?

like image 868
Alex T Avatar asked May 13 '26 22:05

Alex T


1 Answers

You can use what JavaScript has to offer:

const date = new Date('12/14/2020');

so in the context of Cypress:

it("Check date to be less or equal than today", () => {
    cy
      .get('.date', { timeout: 15000 })
      .invoke('text')
      .then(dateText => {
        const date = new Date(dateText);
        const today = new Date();
        
        expect(date).to.be.lte(today);
    });
});
like image 200
pavelsaman Avatar answered May 16 '26 11:05

pavelsaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!