Support creating custom assertion operator like 'should('have.text.trimmed', ...)'
See original GitHub issueCypress have many assertion operators like ‘eq’, ‘contain’, ‘have.text’, but which is not enough for me.
E.g. I want to check a DOM element have some text but want to skip around whitespaces, I have to write:
cy.get('.cell')
.then($cell => $cell.text().trim())
.should('eq', 'Hello')
Since none of have.text
, eq
, contain
really work in this case.
I want to define a custom operator like have.text.trimmed
, to simplify the code to:
cy.get('.cell')
.should('have.text.trimmed', 'Hello')
I can’t find any document about it, does Cypress have a way to do it? Or add this feature?
PS: I also create a question before this issue: https://stackoverflow.com/questions/55842707/how-to-define-a-custom-assertion-operator-in-cypress/55842986
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
How can I define a custom assertion operator in Cypress?
Define a Chai chainable method trimmed . It allows using .should('have.trimmed.text', 'sometext') , which seems to work, but the assertion is ...
Read more >Create Custom Assertions For Test Readability - Gleb Bahmutov
The error message explains that the "have.css" assertion expected an element, but instead received "first" subject. What is this about?
Read more >Creating Custom Assertions - Oracle Help Center
You can develop custom assertions when specific functionality is not provided with the standard policies that come with Oracle Web Services Manager.
Read more >Assertions | Cypress Documentation
This document is only a reference to every assertion Cypress supports. ... You will commonly use these chainers after using DOM commands like:...
Read more >Expect - WebdriverIO
When you're writing tests, you often need to check that values meet ... Every matcher can take several options that allows you to...
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
take a look at https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/extending-cypress__chai-assertions - this is how to add new Chair assertions to the existing ones
We could make it simpler I guess by adding something similar to custom commands
@jennifer-shehane Thanks for the links and I managed to create it by extending chai. I’ve explained the code on stackoverflow, anyone interested may see it there: https://stackoverflow.com/a/55854585/342235