-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Allow cy.contains() to opt out of removing whitespace matching #6405
Comments
Ideally, I was going to build a more comprehensive set of test cases, but I'm short on time and that would probably be part of fixing this anyway. I did notice that in both 3.8.3 and 4.0.1, attempting to just pass a newline (as in,
...which made it difficult to write a comprehensive set of tests around wrapping behavior, in particular asserting that long lines of text in elements with |
I am personally in favor of adding an option for I'm actually not seeing this particular example that you've given passing in 3.8.3. It fails in both 3.8.3 and 4.0.0. Can you provide an exact example that was working in 3.8.3 and is now failing in 4.0.0? I'd like to understand the use case completely that is causing the breaking changes and this doesn't appear to represent it. Thanks! <!DOCTYPE html>
<html>
<body>
<p id="pre" style="white-space: pre;">this
field should preserve all
whitespace</p>
<p id="pre-wrap" style="white-space: pre-wrap;">this
field should preserve all
whitespace</p>
</body>
</html>
describe('css white-space', () => {
before(() => {
cy.visit('index.html');
});
it('should preserve whitespace in the pre paragraph', () => {
cy.get('p#pre').contains('this\n field should preserve all\n whitespace');
});
it('should preserve whitespace in the pre-wrap paragraph', () => {
cy.get('p#pre-wrap').contains('this\n field should preserve all\n whitespace');
});
}); Exact same error in 3.8.3 and 4.0.0 |
Related issue with another point of wanting to opt out of the new ignoring invisible whitespace behavior, this issue matches my point even more of just adding an option to opt-out. #6397 |
I created a new PR for |
Could This would technically be a re-breaking change from 4.0.0 and 4.0.1, but given they were only released a few days ago I wonder if that's ok--it would actually reduce the migration burden for people who haven't upgraded yet, and I don't particularly like the idea of having to clutter every place I include extra whitespace in a string passed to Isn't it safer to assume that if someone passes a string with additional whitespace to As a side note, there are a couple different interpretation of matching whitespace: For an HTML element with a <p>a normal element with some whitespace</p> cy.get("p").contains("a normal element with some whitespace");
cy.get("p").contains("a normal element with some whitespace"); But I would expect the first example to also work if the additional whitespace was present in the displayed HTML. Stripping whitespace from a string I specified, by default, seems to me to violate the principle of least surprise. It seems to me you could potentially get the best of both worlds by, instead of having to have a
At that point, I would want the |
@c32hedge No, we will not be releasing another breaking change for this which would require 5.0.0 release. We believe the default should be to ignore extra whitespaces which is why we merged this change in to begin with. The majority of cases were just extra newlines or spaces that are not effectually rendered for the user to see. Having an option to ignore this, will solve your use case. |
I'm not sure we're on the same wavelength 😁. Referring back to #92, it looks like the original desire was for something like this to work: HTML <h1>hello
world</h1> Test cy.contains("hello world") I didn't see any mention there of expecting the reverse (ignoring whitespace that was explicitly passed to Basically I'm arguing that:
A couple examples to help illustrate: I would consider the expected breaking change in 4.0.0 to be things like the example below, where someone explicitly added whitespace to HTML <h1>hello
world</h1> Test cy.contains("hello\nworld") That would be a good thing to have start failing in 4.0.0, since it highlights code that was depending on the actual whitespace in the source file instead of what the browser actually renders and the user sees. The fix would be to remove the newline from the However, the behavior introduced so that the following that used to work now fails, I would consider a regression, rather than a breaking change that can't be rolled back until another major version: HTML <h1 style="white-space: pre-wrap">hello
world</h1> Test cy.contains("hello\nworld") Sorry, I think the wording of my previous comment wasn't very clear. Also, I'm working on making a |
The first four tests pass in Cypress 3.8.3, but fail in 4.0.1 The last two tests fail in Cypress 3.8.3, but pass in 4.0.1 The desire is that all tests pass
The first four tests pass in Cypress 3.8.3, but fail in 4.0.1 The last two tests fail in Cypress 3.8.3, but pass in 4.0.1 The desire is that all tests pass
Added example repo. |
@c32hedge After reading your comment, it seems to me that what you want is not whether the default value of You want Cypress to pass the tests which check the elements with If I'm reading correctly, it's implemented in #6411. (Click here to see the tests.) But it seems that we cannot support EDIT: Confirmed that one thing is not covered: cy.contains('this field should preserve all whitespace').should('not.exist') // should pass but fails. |
@sainthkh After reading the code in #6411 and some of the surrounding code, yes, this looks like what I want. I realized I was mistaken on how Cypress was handling the text I passed to |
@sainthkh Actually, I'm wondering if this is even simpler. I noticed the following line in the existing code for let testText = elem.textContent || elem.innerText || $.text(elem) Does everything "just work", without all the new logic for looking at the let testText = elem.innerText || elem.textContent || $.text(elem) (see https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent#Differences_from_innerText) In addition, you might want to add a test for something like the example HTML on the Mozilla page describing innerText: <p id="source">
<style>#source { color: red; }</style>
Take a look at<br>how this text<br>is interpreted
below.
<span style="display:none">HIDDEN TEXT</span>
</p> The following test fails in Cypress 4.0.1: describe('this tests what happens if you have style elements inline', () => {
it('should not contain the style code text', () => {
cy.get('#source').contains('color: red').should("not.exist");
});
}); Based on the documentation linked above though, it looks like |
Maybe a new main method be added to Cypress to better express the difference between the "old" contains and the "new" contains? I'm facing a codebase with a few breakages (and currently no solution) following this change, because many of our texts come from wordings in files which are concatenated / rearranged together, which makes us end up with some strings like " It seems that the old I find it weird that this breaking changes, which was specifically announced in the migration guide as "cy.contains() ignores invisible whitespaces", turns out to have an impact that is quite the opposite, and to really be that |
Is there an ETA for the whitespace flag, or any otherchange to this behavior @sainthkh? This change in v4 was very confusing to me. I've made a little repo to show my use case: Basically, I've used contains() to match text distributed over separate elements. Before v4, I could basically count the whitespace between elements to get the string I needed to assert against. |
This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided. |
Bad bot, it still behaves this way and a flag to ignore white space would be helpful unless there is a new command I should be using instead of contains. Some items might have a trailing whitespace, and the easiest way to handle this would be to simply ignore white space and match the text characters only. Any update? |
Hi @DRiewaldt , I will update this feature accordingly with the correct labels to make sure this issue is not picked up as stale in the future. I will also forward this along to our product team for review. Thank you for your response. |
I would love to see, an Update here. The Idea of, having a flag or any other Option to turn this of would be great. |
Current behavior:
Per https://docs.cypress.io/guides/references/migration-guide.html#cy-contains-ignores-invisible-whitespaces,
cy.contains
now ignores extra whitespace. Apparently #5653 only excluded the<pre>
tag from the change, which is not sufficient for modern web applications using arbitrary custom tags.Desired behavior:
cy.contains
should respect an element's white-space css property instead of just looking at whether it's a<pre>
tag.Honestly, I'd like to see the change added in 4.0.0 rolled back, since this is breaking our existing tests that specifically check that whitespace is preserved in some custom elements, and I would argue that the change made was too simplistic to be useful in modern CSS3/HTML5 development.
Alternatively, it would be nice to at least have an option for whether to ignore whitespace, similarly to the option that was added for
matchCase
.Test code to reproduce
Versions
Cypress 4.0.0 and later
The text was updated successfully, but these errors were encountered: