Cypress says
So we can't change the subject behavior when overwriting a command, and We can't add a new command with the same name as a core command (link).
How can I create a find
command with my own custom behavior and an optional subject.
(Note: I would take a different name, but I guess find
and get
are the most readable and are already part of the Cypress)
// this is just an example of a custom find
Cypress.Commands.overwrite('find', (originalFn, selector, options) => {
return cy.log('my custom find')
})
cy.get('body').find('div') // OK
cy.find('div') // throws ERROR (I want this to be possible)
Not sure if you're just giving an example, but
cy.find('div')
(if it were valid syntax) is equivalent tocy.get('div')
becauseget
always searches from<body>
.The whole reason
find
exists is to search from a different element, a shorthand for.within()
In the general case, if you're not using typescript you can just go ahead and add new properties to the
options
parameter and access them inside the overwrite.If you are using typescript, you need to amend the type definition to include the new option.