Cypress gives us the ability to create custom commands and overwrite the existing commands. It comes with its API for creating custom and overwriting existing commands.
Syntax
Usage
Overwriting existing commands
Before version 9.0.0,
Cypress did not warn us when we try to add
a command that already exists
in cypress using Cypress.Commands.add()
and it leads to the unexpected behavior with a command
since it was unclear which would take precedence.
From Cypress 9.0.0,
if we try to add a command that
already exists using Cypress.Commands.add()
,
Cypress will throw an error
indicating that Cypress.Commands.overwrite()
should be used instead to overwrite
the behavior of existing commands.
This change is a breaking one, and it will throw an error if we had previously added any new command which already existed in Cypress.
Note:
It only works with the core Cypress commands. If we try to add a duplicate custom command that we have already defined, then Cypress will not throw an error. If we define more than one custom command with the same name Cypress will execute the last defined command.