Question

Cypress - way to make contains() work in case-insensitive way

I have a project that doesn't capitalize on level of HTML and values in HTML have inconsistent capitalization.

Is there any way how to force Cypress to match text if I use only lower-case strings as arguments to contains() function?

 48  39741  48
1 Jan 1970

Solution

 66

As of 4.0.0, Cypress offers matchCase option with contains...

cy.get('div').contains('capital sentence', { matchCase: false })

2020-05-14

Solution

 15

Cypress contains methods already have such feature. You can just pass the matchCase as options argument and cypress will handle case sensistive/insensitive conditions. Below code snippet will help you. If you like the answer, please like the answer and give vote up.

it('this will pass', () => {
    cy.visit('https://example.cypress.io');
    cy.contains('commands drive your tests', {matchCase: false})
  });

  it('this will fail', () => {
    cy.visit('https://example.cypress.io');
    cy.contains('commands drive your tests', {matchCase: true})
  })
2020-05-14