-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Support creating custom assertion operator like 'should('have.text.trimmed', ...)' #4026
Comments
I would love that too. As I mentioned on stackoverflow it is possible to combine a .contains() with a regular expression. But it would still be a welcome add-on to use regular expressions inside .should() |
Related issues - closing as duplicate
|
@jennifer-shehane not quite duplicate I think. Trimmed text before checking is just an example here, I care more is the ability to create custom operators |
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
|
Ok, that is a broader request. We use chai for our assertions and this is also extensible with chai plugins via these instructions:. https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/extending-cypress__chai-assertions So, I believe creating a chai plugin (or using one of the many existing ones) and including it into cypress may do the trick. Does this satisfy the assertion extension you were looking for? |
@bahmutov beat me 🙃 |
@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 |
@freewind Awesome, and great question. I will close this issue as resolved. |
See https://github.com/Lakitna/cypress-commands for a solution |
Cypress 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:
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: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
The text was updated successfully, but these errors were encountered: