Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare datetime function cypress

Tags:

cypress

I have a form in which a user is allowed to enter the date range and the output will have results form that particular date alone.

I am able to input the date to the form using .type() function. However, I am not sure how to check if the results are within the range specified.

For example, if the typed date is ('17/03/2019') I should be able to check if the results in the table are within the range using a code like this,

cy.get('.table')
  .should('not.contain','18/03/2019') // This is wrong 

I want to compare and see if the results produced are only between 17/03/2019 00:00:00 and 17/03/2019 11:59:59.

Is this feature available in Cypress? Can somebody help, please?

Thanks, Indhu.

like image 800
Indhu Nachadalingam Avatar asked Mar 21 '19 01:03

Indhu Nachadalingam


1 Answers

Just to keep everyone posted on the solution arrived at Cypress Gitter channel. The question is originally answered by Mr. Gleb Bahmutov

This validation can be handled by Cypress.moment, where moment.js library has parsing and comparison methods for date/ time.

// the time in the element should be between 3pm and 5pm
const start = Cypress.moment('3:00 PM', 'LT')
const end = Cypress.moment('5:00 PM', 'LT')

cy.get('.utility-moment .badge')
  .should(($el) => {
    // parse American time like "3:38 PM"
    const m = Cypress.moment($el.text().trim(), 'LT')

    // display hours + minutes + AM|PM
    const f = 'h:mm A'

    expect(m.isBetween(start, end),
      `${m.format(f)} should be between ${start.format(f)} and ${end.format(f)}`).to.be.true
  })

Related document addition: https://github.com/cypress-io/cypress-example-kitchensink/pull/225

like image 99
Kondasamy Jayaraman Avatar answered Sep 30 '22 08:09

Kondasamy Jayaraman