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

Have .contains() strip new lines of targets. #92

Closed
jennifer-shehane opened this issue Dec 11, 2015 · 8 comments
Closed

Have .contains() strip new lines of targets. #92

jennifer-shehane opened this issue Dec 11, 2015 · 8 comments
Labels
good first issue Good for newcomers pkg/driver This is due to an issue in the packages/driver directory type: unexpected behavior User expected result, but got another

Comments

@jennifer-shehane
Copy link
Member

Description
Sometimes when I issue .contains() commands with text I expect to be found, it doesn't match my element as expected due to extra whitespace/new lines in my element.

Code

HTML

<h1>hello
world</h1>

Test

cy.contains("hello world") 
// CypressError: Timed out retrying: Expected to find content: 'hello world' but never did.

Web Page

screen shot 2015-12-11 at 12 05 02 pm

Additional Info

Checking jQuery's .text() in console, I see there is whitespace leading/trailing the actual text.

> $("h1").text()
> "hello
world"

Could it be possible to remove whitespace/new lines of el's when trying to match with .contains?

@jennifer-shehane jennifer-shehane added type: question type: unexpected behavior User expected result, but got another labels Dec 11, 2015
@jennifer-shehane jennifer-shehane changed the title Have .contains() strip leading/trailing whitespace of targets. Have .contains() strip new lines of targets. Dec 11, 2015
@jennifer-shehane jennifer-shehane added the pkg/driver This is due to an issue in the packages/driver directory label Mar 21, 2017
@jennifer-shehane jennifer-shehane added good first issue Good for newcomers stage: ready for work The issue is reproducible and in scope and removed type: question labels Oct 27, 2017
@tejasbubane
Copy link
Contributor

tejasbubane commented Oct 31, 2017

Should it also strip newlines/whitespaces between words or just beginning and trailing of the entire string?

@hlb-schmidt
Copy link

hlb-schmidt commented Aug 1, 2018

hmm, the title is "Have .contains() strip new lines of targets". but reading the description, shouldn't the title be "Have .contains() replace new lines with space"?

and for the record, as the documentation for .contains() sais "DOM elements can contain more than the desired text and still match", so doing a .contains("hello world") will also match an element <p>what hello world ever</p>.

ps. i dont think automatically replacing anything other than leading/trailing whitespace in a string is a good idea. (removing would be catastrophic: hello\nworld -> helloworld)

@plehnen
Copy link

plehnen commented Oct 26, 2018

I also would like to have it ignoring whitespace.
May be an alternative solution would be to include chai-string. e.g. "equalIgnoreSpaces"

@jennifer-shehane
Copy link
Member Author

@plehnen Interesting, I didn't know this existed. You could include chai-string in Cypress and use this assertion if it's a blocker for anything. See this example on how to extend chai.

@plehnen
Copy link

plehnen commented Nov 27, 2018

@jennifer-shehane I do include chai-string. But it doesn't work as easy as in the example.
I do something like this in my spec file for a workaround:

import chai from 'chai';
import chaiString from 'chai-string';

chai.use(chaiString);

// this is a hack, because it seems that cypress does not really support 3rd party chai plugins yet
const { expect } = chai;

const hasTextIgnoreSpaces = (selector, text) => {
  cy.get(selector).invoke('text')
    .should(t => expect(t).to.equalIgnoreSpaces(text));
};

@RhinocerosUnicornis
Copy link

RhinocerosUnicornis commented Oct 24, 2019

A simple workaround for now is to use regex. For example, I have a span with "50 widgets" in HTML template like this:

<span id="my-quantity-of-widgets-a8bc90dd">50
widgets</span>

I can match it with contains still by shipping it a regex.

cy.get('span[id^="my-quantity-of-widgets-a8bc90dd"]').contains(/50(\s)*widgets/);

Relevant code chunk in the driver:

@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 cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Jan 30, 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 Author

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
good first issue Good for newcomers pkg/driver This is due to an issue in the packages/driver directory type: unexpected behavior User expected result, but got another
Projects
None yet
Development

No branches or pull requests

5 participants