Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

620
Views
How to assert today's date or older in Cypress

I am trying to write a function in Cypress, where it loops through all the columns for my start date (#EventStart) and makes sure that the start dates are today's current date or older. So for instance, I need this test to make sure that it is confirming that all the dates listed are either today's date (April 4) or earlier (April 3, 2, 1, etc, etc). So far I can get it to confirm today's date, but how can I add to the function so that it also looks for the days prior to today? Also, the way it is written, it is getting me GMT Time, how can I get it to display CST?

EDIT: This is how the date displays: April 4, 2022, 1:00pm

  public assertOldDate () {
    cy.get("#EventStart).each((el) => {
     let oldDate = new Date();
      oldDate.setDate(oldDate.getDate())
      cy.get("el").should("equal", oldDate);
    }
  }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using dayjs to parse the element text,

import dayjs from 'dayjs'

const today = dayjs()

cy.get('[id*="EventStart"]')
  .each($eventStart => {

    const dateText = $eventStart.text()

    Cypress.log({message: `Testing ${dateText}`})               // log as per web page
    const formatted = dateText.split(', ').slice(0,2).join(' ') // remove commas & time 
    const date = dayjs(formatted, 'MMMM DD YYYY')               // parse the text

    expect(date <= today).to.eq(true)
  })

Note, changed id selector to [id*="EventStart"] as id's are expected to bu unique. Using #EventStart seems to block the iteration.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!