Search code examples
javascriptmocha.jscypressassert

How to use logical OR in Cypress should assertion


I have this code

cy.get(element).should('contain.text', search_word || search_word.toLowerCase())

And I get this error

expected <div.games__element__title.card-title.h5> to contain text Hot, but the text was Ultimate hot

How can I use OR operator, so I can assert that the text of element contains the searching word written either in uppercase or in lowercase letters?


Solution

  • For text comparison, instead of using OR approach, I suggest using lowercase comparison by having the expected text and actual text to be compared in the lowercase version. This is a cleaner approach.

    cy.get(element).invoke('text').should(text => {
      expect(text.toLowerCase()).to.contain(search_word.toLowerCase());
    })
    

    another alternative is using regex

    cy.get(element).invoke('text').should('match', /(h|H)ot/);