https://cypress.io logo
Best practices on creating re-usable scenarios for...
# i-need-help
t
Hi! In the organisation I'm currently working in we opted for Cypress as our E2E testing tool. Lately, we're developing a mission critical feature that requires jumping among 3 different sections while compiling several forms before getting to the page I want to test. In order to speed up the test and make it more readable/maintainable, I was thinking about creating several scenarios that perform multiple requests to our BE in order to create the application state. It would look something like this: - perform a login; - create an item into the warehouse; - add it to a cart; - delete item for availabilities; then I want to the test cart and simulate a real user interaction. Everything else is just pre-populating the application state so it would be incredibly long to setup the test using the UI. Is it a reasonable approach? Are there any available resources in the official documentation I can look at? Thanks, Maurizio
e
This is absolutely a reasonable approach and a best practice. You should take advantage of
cy.session
in a custom login command if you haven't already. Then create some helpers that make your API calls to setup the state of your application using
cy.request
. That way when you visit the application you are in the state you need to be in. I drafted some pseudo code of maybe what this would look like for you but it's just one interpretation. I'd be happy to discuss further if you have other questions.
t
Hey! Thanks for the answer ❤️ Your pseudo-code summarised pretty well what I was currently doing. Glad to know I'm on the right track!
Is there any way I can reuse redux store/actions so I just have to setup the payload for each API calls?
e
I created a custom command awhile ago to dispatch my own redux actions as part of the setup for certain tests. Then I just wrapped that custom command with other methods for each specific redux action and payload already set. So something like
dispatchOnboardingInfo
or whatever. You are more than welcome to use it if it helps. Does that answer your question? ``` * Action that dispatches redux actions to the redux store * @param {Object} action redux store action */ Cypress.Commands.add('dispatch', (action) => { Cypress.log({ name: 'dispatchReduxAction', }); cy.window().its('store').invoke('dispatch', action); });
4 Views