question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

should('have.text', ...) should ignore leading and trailing whitespace

See original GitHub issue

If your HTML happens to have whitespace in the tag, e.g., <div> foo </div>, that text will not impact how the user sees the page, unless it is a <pre> element. I don’t think that the 'have.text' assertion should fail just because it found ' foo ' when it was expecting 'foo'.

If you’re not willing to change this (since it is technically a breaking change), maybe there should be a have.text.trimmed?

Current behavior:

should('have.text', ...) fails if the HTML contains whitespace, even if that whitespace doesn’t impact the rendered content.

Desired behavior:

should('have.text', ...) should ignore leading and trailing whitespace for elements that won’t render it. Elements like pre (and elements with certain white-space css values) should still consider leading and trailing whitespace.

Versions

3.2.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:57
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

40reactions
x-yuricommented, Sep 8, 2019

One way to go about it is:

cy.get('...').should($el => expect($el.text().trim()).to.equal('...'));
29reactions
paurakhsharmacommented, Dec 18, 2019

I also encountered the same issue so I use:

cy.get('.message').should('contain.text', message)
Read more comments on GitHub >

github_iconTop Results From Across the Web

5.12. Trim Leading and Trailing Whitespace - O'Reilly
Removing leading and trailing whitespace is a simple but common task. The regular expressions just shown contain three parts each: the shorthand character...
Read more >
Why is trailing whitespace a big deal? [closed]
When I have string literals that span multiple lines, trailing whitespace can make the output string look incorrect when it's used. While not ......
Read more >
are leading and trailing whitespaces ignored in html?
No, line-breaks and white-space are both rendered the same way in HTML tags. – Drazzah. Nov 19, 2014 at 20:09. Let us put ......
Read more >
Remove Leading/Trailing Whitespace - R
Remove leading and/or trailing whitespace from character strings. ... "r") ## Unicode --> need "stronger" 'whitespace' to match all : tt <- "text...
Read more >
How to remove all leading whitespace in string in Python?
We can also use the replace() method from the string library to remove the leading spaces. In this approach, we will replace all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found