0

This is my code -

const username = faker.internet.userName()


describe('My test suite', () => {
  it('test case 1', () => {
    cy.visit('https://google.com')
    cy.log(username)
  })

  it('test case 2', () => {
    cy.visit('https://facebook.com')
    cy.log(username)

  })

The value of constant variable changes if the URL domain is changed in test cases. Is there anyway we can retain the constant values across domains?

Attaching cypress output for the code -
Cypress output

Fody
  • 23,754
  • 3
  • 20
  • 37

1 Answers1

1

You will observe that the browser resets between domains.

Cypress completely restarts the test runner browser when you give it a new domain in the second test, and all local variables such as username are re-initialized.

To preserve the same value, you could use a task such as here - be careful about passing the parameters, in that question the code was used incorrectly.

The reason why a task works is that only the browser is reset between domains, not the background Node process.

Or you could save it to a fixture file as per this answer Visiting two different websites and storing value in variable.

Fody
  • 23,754
  • 3
  • 20
  • 37