Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and 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 Reply

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);
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...