Ale*_*x T 2 javascript e2e-testing cypress
我正在尝试检查从应用程序中的元素获取的一个日期值是否小于今天的日期:
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);
})
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Timed out retrying after 4000ms: expected '12/14/2020' to be a number or a date
Run Code Online (Sandbox Code Playgroud)
有没有办法将从元素获取的日期转换为日期时间对象?
您可以使用 JavaScript 提供的功能:
const date = new Date('12/14/2020');
Run Code Online (Sandbox Code Playgroud)
所以在 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);
});
});
Run Code Online (Sandbox Code Playgroud)