I am working with cypress and can’t find a way to carry my current state to another iteration within a spec file and i have multiple specs file.
for Eg:
I opened a staff list, and i clicked on add staff button. A pop up form open and I just created a staff and form closes with success toaster and I am back on the staff list. The iteration script ends with it and in my next iteration I want to search that staff which i created but the issue I face is that I had to give him cy.visit() command to take me back to that staff list.
I am unable to find a solution and kind of stuck
2
Answers
Having tests that are not independent of other tests is not considered a best practice in Cypress (and I’d argue, almost any other testing tool.) This Cypress blog post gives some good reasons why it is a best practice, as well as the Cypress core-concept documentation.
Instead, I’d suggest creating a function that can create the staff member without the use of the UI.
Why should the second test create it’s own test data? There are a few reasons, but the most compelling for me is that Test 2 will fail if Test 1 fails. If Test 1 fails to create the data, then obviously, Test 2 will fail. You’ll have cascading failures for any tests that have these dependencies on a test, and the singular reason why won’t always be obvious. If Tests 1 and 2 are independent, you’ll have an easier time debugging failures.
However, if, for whatever reason, it is not feasible/reasonable/possible to have Tests 1 and 2 be isolated from one another, you can disable Test Isolation. An alternative would be to combine Tests 1 and 2 into a singular test (which, while less atomic than the alternative, still seems to be a better solution than disabling Test Isolation.)
The package cypress-data-session looks like a good fit.
It allows you to be explicit about the data that is carried from one test to another, at the same time preserves integrity of test isolation.
Example: