Have .contains() strip new lines of targets.
See original GitHub issueDescription
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
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:
- Created 8 years ago
- Reactions:6
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A simple workaround for now is to use regex. For example, I have a span with “50 widgets” in HTML template like this:
I can match it with
contains
still by shipping it a regex.Relevant code chunk in the driver: https://github.com/cypress-io/cypress/blob/e5c33fe56814c9559913d88f07086993cc0b9f39/packages/driver/src/cy/commands/querying.coffee#L291
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.