Cypress 'expect' with 'regex' fails to validate on second occurrence.
See original GitHub issueCurrent behavior
Steps to reproduce:
- Read a text from any web page.
- Use
expect
tomatch
aregex
. - Now, read the same data from the web page again.
- Use
expect
tomatch
aregex
.
Actual result: Validation passes the first time but the same validation fails the second time.
Desired behavior
The validation should pass each time if the condition is satisfied or report the appropriate failure if the condition fails.
Test code to reproduce
/* eslint-disable cypress/no-unnecessary-waiting */
/* eslint-disable no-unused-vars */
/// <reference types="Cypress" />
it("Testing Table Validation", () => {
let regex = /Germany|UK/gm;
cy.visit("https://www.w3schools.com/html/html_tables.asp");
cy.get("table[id='customers'] tbody tr:nth-child(2) td:nth-child(3)")
.then(element => {
cy.log(element.text());
expect(element.text()).to.match(regex); // This validation works fine.
});
cy.wait(2000);
cy.get("table[id='customers'] tbody tr:nth-child(2) td:nth-child(3)")
.then(element => {
cy.log(element.text());
expect(element.text()).to.match(regex); // This validation fails.
});
});
Cypress Version
8.5.0
Other
Am I doing something wrong here?
The same scenario works if I use should
instead of expect
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Cypress - use of regular expression in 'contains' function ...
contains(/^Tier2_Forest/) fails because the text is not at the beginning of the string, there is white space preceding it. Just try .contains(/ ...
Read more >Assertions | Cypress Documentation
Cypress bundles the popular Chai assertion library, as well as helpful extensions for Sinon and jQuery, bringing you dozens of powerful assertions for....
Read more >Assertions | Cypress examples (v9.4.1) - Gleb Bahmutov
Match numbers using a regular expression. Imagine we want to validate the date inside an element. The date should have the day number...
Read more >What are Cypress Assertions and How to use ... - Tools QA
Otherwise, it will not be feasible to validate whether the application reached the expected state or not. Cypress itself bundles assertions ...
Read more >How to handle Cypress Assertions : Tutorial - BrowserStack
For every test, it is essential to have a validation that checks whether it functions as expected or not. Assertions are these validations ......
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
I’m going to close this as there’s a solution and I don’t think we should interfere in the behavior of regular expressions for this case. It’s really more of an issue with JavaScript itself and not Cypress, similar to needing to do a deep comparison for two object references (e.g.
expect({}).to.equal({})
vsexpect({}).to.deep.equal({})
).Thanks Chris. That makes it clear.