Limitations | Tricks of Cypress: Case-insensitive

I would call it as a limitation of the tool, but it’s rather the limitation of my own knowledge.

It’s because the way how I would use the tool intuitively is actually wrong.

It can happen that there is a mismatch in capitalization between the expected and actual text. There is a scenario in which it is completely acceptable to have this mismatch, and you don’t want your assertion to fail on this.

In my case the expected text was all in upper case letters, while the actual text had the very first letter in upper case.

How would you assert on the text content only?

One proper solution that is offered:

cy.get('div').contains('expected text', { matchCase: false });

Other possibility is to use Regex:

cy.get('div').contains(/expected text/i);

And the third one, which won the tender in my case:

cy.get('div').should(($el) => {
let text = $el.text().toUpperCase();
expect(text).to.include(someText);
});

Why did I use include instead of match?

Yes… that is another story…

Spoiler alert: complexity is the name of the game.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s