Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15935

Way to share data between two test runs in Cypress targeting different origins

In cypress

  1. I am trying to create a random number, in one spec, posting it to the first website

  2. Navigate to another website with different origin, want to assert that this random number generated in the first test, is available on the second website since they are linked in the backend

This won't happen as same code to create random number would run again, in case we generate in before() block, thus creating and assigning another random number to that variable, since before() block will run again in case we visit another url

few ways like using -

  1. describe function scope --> will run again and assign new random number
  2. static class properties --> will run again ...
  3. java singleton principle --> will run again ...
  4. cypress commands in cypress/support/command.js --> will run again ...
  5. local storage --> doesn't work, local storage gets cleared when visiting another url

None of them worked, although if we were not to visit another url, all of them would work normally

Upvotes: 0

Views: 2086

Answers (1)

Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15935

This is documented here https://github.com/cypress-io/cypress/issues/2636 that it is not possible to use cy.visit to visit different origin websites and maintain state

I have two solutions for such scenarios

  1. use cy.writeFile to write in the first run, and cy.readFile to read in next run, since it actually writes to file system, this gets persisted and thus solves our problem, we are able to maintain state
  2. Use JSON file for data, and thus cy.fixture().then(str => str) which will remain common across both test runs

Upvotes: 1

Related Questions