admin管理员组文章数量:1316980
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?
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?
Share Improve this question asked Jan 12, 2021 at 15:00 Alex TAlex T 3,76415 gold badges69 silver badges126 bronze badges2 Answers
Reset to default 6You 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);
});
});
The moment library is deprecated, using dayjs is remended instead. You'll need this when parsing custom date formats, which might not be supported by the Javascript Date constructor. Based upon your error message I assume the expected format should be MM/DD/YYYY
instead of DD/MM/YYY
.
it("Check date to be less or equal than todays", () => {
cy.get('.date', { timeout: 15000 }).invoke('text').then(actualDateText => {
const dayjs = require('dayjs');
const todaysDate = new Date();
const actualDate = dayjs(actualDateText, 'MM/DD/YYYY').toDate();
expect(actualDate).to.be.lte(todaysDate);
});
});
本文标签: javascriptComparing two date values in cypressStack Overflow
版权声明:本文标题:javascript - Comparing two date values in cypress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742016247a2413826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论