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
Namespace for custom commands #6575
Comments
@jennifer-shehane I am facing a scenario similar to the one described by @brettz9 |
Hey |
It could be a great feature to allow namespaces to custom commands! |
How would this work for long chains? Let's say I have a cy.get('button').click().custom.myCustomGet().custom.myCustomGet() This seems quite verbose and cumbersome. Is this the desired syntax, and in line with the request?
This seems unlikely, the product is quite mature and we don't often add new commands at this point.
This seems like a more compelling argument. I think it's easy to find out something is built in (just look at the docs). A short term solution might just be to have a convention, eg: const custom = {
get: ...,
login: ...
}
Cypress.Commands.add("customGet", custom.get)
Cypress.Commands.add("customLogin", custom.login) The usage would be: // proposed w/ namespaces
cy.custom.get('button').custom.login()
// without namespaces, using convention
cy.customGet('button').customLogin() I could be misunderstanding how the namespaced commands would be used. Maybe someone can share their desired API? |
It is not absolutely trivial to have to look something up in the docs, though Cypress' TypeScript support can help with this. I guess enforcing a I'm ok with the issue being closed. |
Thanks for the feedback. While it would be great to iterate on this feature request, settle on an API and push forward with implementation, I think given the other higher priority feature requests, and the complexity here, it is unlikely we will get to this in a reasonable time frame. I don't want to set an unrealistic expectation that we will work on this soon, only to not make meaningful progress for months. Maybe we can revisit this in the future - the main blocker I see with this feature is the API design. If anyone has a great proposal, happy to re-open and discuss further. If anyone wants to take a stab at this, make sure to consider these comments and edge cases.
We should make docs trivial to use - everything about the API is here. If you have some ideas to improve docs, that's something we can do much more easily, since it doesn't require the same level of thought and design a new core API does. Happy to discuss improvements to discoverability in the docs repo - if you've got ideas, please open an issue and tag me and @elylucas and we can work on it. |
FWIW, I didn't mean that the docs weren't excellent. I was just saying that it can be distracting to have to leave one's code to look something up (even if you have a browser shortcut set for the Cypress site like I do to make searching even faster). But indeed, naming conventions and TypeScript do help avoid the need to leave the code to consult the docs. |
This would be nice. Not only to separate custom commands from Cy commands, but also to namespace your own commands. Can we reopen? |
If this can't be supported by cypress natively, could someone provide a way to namespace commands? Cypress.Commands.add('modal', () => {
return cy.get('.modal').then((modalContext) => {
const actions = {
button: (label) => {
const buttonSelector = `button:contains('${label}')`;
return {
click: () => modalContext.find(buttonSelector).click(),
};
},
};
return cy.wrap(actions);
});
}); The idea was to have an api like cy.modal().button('Label').click() This is not cumbersome at all IMO. Again, this doesn't need to be supported natively by Cypress. If the cypress team can come up with a way to do it I would be happy as well. |
Current behavior:
Desired behavior:
cy.custom.myCommand()
Test code to reproduce
N/A
Versions
Cypress 4.0.2, Mac 10.15.2, Firefox 74.0b7
The text was updated successfully, but these errors were encountered: