Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

javascript - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 todays", () => {
    cy.get('.date', { timeout: 15000 }).invoke('text').then(dateText => {
        const date = new Date(dateText);
        const today = new Date();
        
        expect(date).to.be.lte(today);
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...