Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cypress

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?

like image 294
Jar Avatar asked Nov 16 '18 15:11

Jar


People also ask

How do you ignore case sensitive in Cypress?

Cypress contains methods already have such feature. You can just pass the matchCase as options argument and cypress will handle case sensistive/insensitive conditions.

How do you use contain in Cypress?

contains() is chained off of a command that yielded the <button> , Cypress will look inside of the <button> for the new content. In other words, Cypress will look inside of the <button> containing "Delete User" for the content: "Yes, Delete!", which is not what we intended.

How do I get an element's text contents in Cypress?

How do I get an element's text contents? Cypress commands yield jQuery objects, so you can call methods on them. If the text contains a non-breaking space entity &nbsp; then use the Unicode character \u00a0 instead of &nbsp; .

How do you verify text in Cypress?

Cypress can validate the text on an element with the help of jQuery text() method. This method shall help us to fetch the text content on the selected element. We can also put assertions on the text content of the element. cy.


2 Answers

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

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

like image 136
sfletche Avatar answered Sep 21 '22 01:09

sfletche


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})   }) 
like image 35
Srinu Kodi Avatar answered Sep 22 '22 01:09

Srinu Kodi