Skip to content
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

Closed
freewind opened this issue Apr 25, 2019 · 10 comments
Labels
pkg/driver This is due to an issue in the packages/driver directory type: feature New feature that does not currently exist

Comments

@freewind
Copy link

freewind commented Apr 25, 2019

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:

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

@jevors
Copy link

jevors commented Apr 25, 2019

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()

@jennifer-shehane
Copy link
Member

Related issues - closing as duplicate

@jennifer-shehane jennifer-shehane added the type: duplicate This issue or pull request already exists label Apr 25, 2019
@freewind
Copy link
Author

@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

@jennifer-shehane jennifer-shehane added type: feature New feature that does not currently exist stage: proposal 💡 No work has been done of this issue pkg/driver This is due to an issue in the packages/driver directory and removed type: duplicate This issue or pull request already exists labels Apr 25, 2019
@bahmutov
Copy link
Contributor

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

Cypress.Assertions.add('...', )

@jennifer-shehane
Copy link
Member

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?

@jennifer-shehane
Copy link
Member

@bahmutov beat me 🙃

@bahmutov
Copy link
Contributor

bahmutov commented Apr 25, 2019 via email

@freewind
Copy link
Author

freewind commented Apr 25, 2019

@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

@jennifer-shehane
Copy link
Member

@freewind Awesome, and great question. I will close this issue as resolved.

@jennifer-shehane jennifer-shehane removed the stage: proposal 💡 No work has been done of this issue label Apr 26, 2019
@robmint
Copy link

robmint commented Jun 22, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/driver This is due to an issue in the packages/driver directory type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests

5 participants