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?
Cypress contains methods already have such feature. You can just pass the matchCase as options argument and cypress will handle case sensistive/insensitive conditions.
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? Cypress commands yield jQuery objects, so you can call methods on them. If the text contains a non-breaking space entity then use the Unicode character \u00a0 instead of .
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.
As of 4.0.0, Cypress offers matchCase
option with contains
...
cy.get('div').contains('capital sentence', { matchCase: false })
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}) })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With