If you want to ensure that your time-related tests are extremely reliable, consider utilizing the cy.clock()
command. This will effectively halt the app's clock, ensuring the accuracy of any time values retrieved.
This means that even if there are delays in your test execution, the time value obtained from dayJs will remain accurate and consistent throughout.
One important factor to be mindful of is when the time value is actually set within the element. If the app makes API calls to update the time display, it is crucial to strategically capture the timeNow
value to avoid discrepancies.
Although your query lacks detail, refer to the documentation for a better understanding. The example provided in the documentation offers clarity.
cy.clock()
cy.visit('/index.html')
cy.tick(1000)
cy.get('#seconds-elapsed').should('have.text', '1 seconds')
cy.tick(1000)
cy.get('#seconds-elapsed').should('have.text', '2 seconds')
Furthermore, using .contains()
may not be the most suitable approach for this scenario. As you need to dynamically select elements based on the variable timeNow
, opting for .should()
with a within assertion would be more appropriate.
cy.get('#datetime')
.invoke('parseInt')
.should('be.within', timeNow, timeNow + 60000)
It is essential that the timeNow
value is numerical to achieve accurate comparisons.