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.

Have .contains() strip new lines of targets.

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:6
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
RhinocerosUnicorniscommented, 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: https://github.com/cypress-io/cypress/blob/e5c33fe56814c9559913d88f07086993cc0b9f39/packages/driver/src/cy/commands/querying.coffee#L291

0reactions
jennifer-shehanecommented, Feb 10, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove line breaks from start and end of string - Stack Overflow
Try this: str = str.replace(/^\s+|\s+$/g, '');. jsFiddle here.
Read more >
Python String Contains Tutorial with .find(), .index() & .count()
count() method searches for a specified substring in the target string. It returns the number of non-overlapping occurrences. In simple words, how many...
Read more >
How to Check if a Python String Contains a Substring
Python provides many additonal string methods that allow you to check how many target substrings the string contains, to search for substrings ...
Read more >
Python Trim String - rstrip(), lstrip(), strip() - DigitalOcean
lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string. All of these methods ......
Read more >
Python | Removing newline character from string
Method 2: Use the strip() Function to Remove a Newline Character From ... Actual string: Geeks for Geeks After deleting the new line:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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