contain.text and include.text should do substring matches but instead do full-string matches
See original GitHub issueCurrent behavior:
// cy.get('div#preview').should('have.text', 'Hello'); // this is a full match, but we want a partial match, to ignore newlines etc.
// cy.get('div#preview').should('contain.text', 'Hello'); // bug: this does the same as have.text
// cy.get('div#preview').should('include.text', 'Hello'); // bug: this also does the same as have.text
cy.get('div#preview').then((el)=> {
assert.include(el.text(), 'Hello'); // this works but it isn't pretty
});
the log for all three of the above says:
ASSERT: expected <div#preview> to have text Hello, but the text was Hello\n
which makes me think that there might be some aliasing going on since I’d think the error messages for “include” and “contain” would say “include” and “contain”, not "have
Desired behavior:
have.text
should test whether the element’s full text is equal to the given stringcontain.text
andinclude.text
should test whether the element’s text contains the given string but is not necessarily equal to it
This follows the Chai docs here: http://www.chaijs.com/api/bdd/#method_include “When the target is a string, .include asserts that the given string val is a substring of the target.”
Steps to reproduce:
- Clone this repo (branch name
solution
): https://github.com/BurlingtonCodeAcademy/markdown_preview/tree/solution - Run
npm install
- Run
npx cypress open
- Uncomment lines in
cypress/integration/simple_spec.js
and check the cypress log
Versions
- Cypress 3.0.1
- MacOS High Sierra
- Google Chrome / Chromium as installed by
npm
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:22 (8 by maintainers)
Top Results From Across the Web
Cypress: How to check if the element has a text but the text is ...
Another way, test for any text of non-zero length cy.visit('http://example.com') cy.get('h1').invoke('text').should('have.length.gt', ...
Read more >if specifc line contains substring then output other substrings
I used the following text as sample (modified the sample provided by the OP): <TEST1> <text:p text:style-name="P4">Hello<text:span ...
Read more >re — Regular expression operations — Python 3.11.1 ...
Source code: Lib/re/ This module provides regular expression matching operations ... Regular expressions can contain both special and ordinary characters.
Read more >Element selectors | Playwright 中文文档
Text body can be escaped with single or double quotes for full-string case-sensitive match instead. This means text="Login" will match ...
Read more >GREL functions - OpenRefine
contains ("ee") returns false. You can search for a regular expression by wrapping it in forward slashes rather than quotes: "rose is a...
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
Ok, here is example - either explicit or using regular expression
The reason this isn’t working is because the subject / target is a DOM element, not a string.
I’m not sure this is actually a bug, since we vendor in chai (it does what it does), but we did write our own chai assertions for DOM elements here: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cypress/chai_jquery.coffee
I would start diving into what
contain.text
andinclude.text
actually trigger - whether its chai methods or the chai jquery stuff we wrote to understand it better.All of this works though you just have to drill into and change the subject to be a string:
If you’d like to do the legwork to see what assertion is being applied to a DOM element that would be great.