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?
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?
Share Improve this question edited Jun 21, 2021 at 13:55 msanford 12.2k13 gold badges71 silver badges98 bronze badges asked Sep 29, 2020 at 8:51 Ara GalstyanAra Galstyan 5562 gold badges4 silver badges12 bronze badges2 Answers
Reset to default 5For text parison, instead of using OR approach, I suggest using lowercase parison by having the expected text and actual text to be pared 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/);
One way to achieve what you are looking for, is by using the Conditional statement in cypress. We will get the inner Text from the element and then check whether the word hot or Hot is present in the text and based on that we will perform actions.
cy.get(element).invoke('text').then((text) => {
if (text.includes('Hot')) {
//Do Something
}
else if (text.includes('hot')) {
//Do Something
}
else {
//Do Something
}
})