0

I'm trying to check if a set of numbers is a date (TT.MM.YYY) form.

describe('checking if element contains date ', IPHONE_X, () => {
    cy.get('element').contains('numbers between 1-31 . numbers between 1-12 . numbers between 1990 - 2025')
})

I hope you understood what I mean.

Thanks a lot!

Alapan Das
  • 17,144
  • 3
  • 29
  • 52
M T
  • 13
  • 3

1 Answers1

0

You can use a Regex to validate this. I have referred to the regex mentioned in this Stackoverflow answer.

Using Contains. This will not create an assertion.

cy.get('#hello')
    .contains(/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/)

For creating an assertion, You can use should('match', RegEx)

cy.get('#hello').invoke('text')
    .should('match', /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/)

I have run both of the above cases locally and both of them are working.

Test Runner execution

Some of the Test cases(Date Formats) I tried with the Regex, you can check them out here - https://regex101.com/r/duOgew/3

Alapan Das
  • 17,144
  • 3
  • 29
  • 52