Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce non case sensitive matching for cy.contains() #2785

Closed
James-E-Adams opened this issue Nov 15, 2018 · 10 comments
Closed

Introduce non case sensitive matching for cy.contains() #2785

James-E-Adams opened this issue Nov 15, 2018 · 10 comments
Labels
existing workaround good first issue Good for newcomers pkg/driver This is due to an issue in the packages/driver directory type: feature New feature that does not currently exist

Comments

@James-E-Adams
Copy link

Current behavior:

cy.contains('Capital Sentence') does not match

<span class="capitalize"> capital sentence </span>

where:

.capitalize {
   text-transform: capitalize;
}

Is this expected?

Desired behavior:

I assumed it would match because that's what the user sees. However the underlying text in the DOM is indeed capital sentence.

Steps to reproduce:

Versions

Cypress: 3.0.1
Chrome, Mac OS Mojave

@brian-mann
Copy link
Member

It would be the same problem in regards to things like uppercase or truncate for that matter.

If you know of any DOM methods that return the text with this kind of CSS styling factored in, let us know.

@James-E-Adams
Copy link
Author

James-E-Adams commented Nov 15, 2018

Yeah it's a tough one.

A possible algorithm for case-sensitivity using text-transform (wouldn't fix truncate using text-overflow) could be:

  • check if the provided string to cy.contains() has a direct match in the DOM
  • otherwise check if the string.toLowerCase()/string.toUpperCase() has a match in the DOM, compute the match with its text-transform property applied and see if it matches the original string

But it's less efficient because it requires multiple traversals of the DOM so it could potentially be opt-in?

@jennifer-shehane
Copy link
Member

Introducing this into the default cy.contains() would also make it impossible to then test actually capitalization within an application. If my team wanted our content to move from Capitalized to Sentence Case, for example, there wouldn't be a way to test that those changes were put in correctly.

If this were even implemented, I would recommend it as a separate argument to be passed like in 'search' in your IDE - to matchCase or noMatchCase.

@jennifer-shehane
Copy link
Member

I actually might prefer to have matchCase as the arg and no match case as the default because I get pretty annoyed myself having to update my tests when I only changed capitalization in my app.

@jennifer-shehane jennifer-shehane added the type: feature New feature that does not currently exist label Nov 19, 2018
@jennifer-shehane jennifer-shehane changed the title Potentially wrong behaviour with text-transform: capitalize Introduce non case sensitive matching for cy.contains() Nov 19, 2018
@vedmant
Copy link

vedmant commented Feb 7, 2019

I'd also need case insensitive contains method, maybe add some options to it?

@jennifer-shehane jennifer-shehane added stage: ready for work The issue is reproducible and in scope pkg/driver This is due to an issue in the packages/driver directory difficulty: 1️⃣ labels Feb 14, 2019
@Bahaa-Addin
Copy link

Bahaa-Addin commented Jun 11, 2019

I solved it using a regex with i flag for case insensitive :
cy.contains(/capital sentence/i)

@vesper8
Copy link

vesper8 commented Sep 7, 2019

please add an argument to make cy.contains optionally work in a non case-sensitive way

using the regex works but when combined with variables requires you to add a lot of extra code.. it would be much better to have it built-in

@vesper8
Copy link

vesper8 commented Sep 7, 2019

my solution is to create a helpers class and then import it inside my tests like this:

helpers.js

export default class Helpers {
  static caseInsensitive(str) {
    // escape special characters
    this.input = str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    return new RegExp(`${this.input}`, 'i');
  }
}

spec.js

import helpers from '../support/helpers';
...
cy.get('[data-cy="form-errors"]').contains(helpers.caseInsensitive(variables.strings.en.general.oops));

@cypress-bot cypress-bot bot added stage: work in progress and removed stage: ready for work The issue is reproducible and in scope labels Nov 11, 2019
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Dec 19, 2019
@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: needs review The PR code is done & tested, needs review stage: work in progress labels Jan 27, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 30, 2020

The code for this is done in cypress-io/cypress#5653, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@jennifer-shehane
Copy link
Member

Released in 4.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v4.0.0, please open a new issue.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Feb 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
existing workaround good first issue Good for newcomers pkg/driver This is due to an issue in the packages/driver directory type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests

6 participants