skip to Main Content

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


  1. 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.

    // This is just a pseudo example
    describe('My tests', () => {
      beforeEach(() => {
        cy.visit('/foo');
      });
    
      it('validates users can create staff members', () => {
        // code to validate user can create staff members via UI
      });
    
      it('validates created staff members appear on the webpage', () => {
        cy.createStaffMember(); // custom helper function you've written to create the staff member 
        // the ideal scenario would be that the above helper function does not use the UI
        
        // remaining portion of the test that validates staff member appears on the webpage 
      });
    });
    

    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.)

    Login or Signup to reply.
  2. 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:

    beforeEach(() => {
      // let's say you want to set up the value "A"
      cy.dataSession(
        'A', // data name
        () => 'a', // data creation commands
        (x) => x === 'a', // data validation function
      )
    })
    
    it('has object A', () => {
      expect(Cypress.getDataSession('A')).to.equal('a')
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search