What is the best way to assert that a component contains a string "case insensitive" using Cypress?

Viewed 121

I want to assert that a component contains a string without caring about the string case.

For example, I want

cy.get('#label').should('contain.text', 'integrator');

to pass even if the label contains "Integrator."

What is the best way I can make this assertion?

4 Answers

What you need is Regular expressions. You can use the match assertion:

cy.get('#label')
  .invoke('text')
  .should('match', /integrator/i) //i = case sensitive 

You can also use cy.contains() with a regular expression

cy.contains('#label', /integrator/i)  // should is implied in this command

or as an option

cy.contains('#label', 'integrator', {matchCase:false})

With should() you get retry of the expect()

cy.get('#label')
  .should($el => {
    expect($el.text().toLowerCase()).to.eq('integrator')  // exact
    // or
    expect($el.text().toLowerCase()).to.contain('integrator')  // partial
  })

You can do like this as well:

cy.get('#label').then(($ele) => {
  expect($ele.text().toLowerCase()).to.contain('integrator')
})
Related