1

I need to pass the url and other variable in multiple tests[it-function]. For 1st test code run successfully but for 2nd test it showing error. Is there any workaround or solution? My code is as follows

describe('Document Upload', function()
{
    before(function () {
        cy.fixture('Credential').then(function (testdata) {
            this.testdata = testdata
        })
    })
    //1st test
    it('Login as manager',function()  
    {
    const login = new loginPage()

    cy.visit(this.testdata.baseUrl);
    login.getUserName().type(this.testdata.userDocumentM)
    login.getPassword().type(this.testdata.passwordDocumentM)
    login.getLoginButton().click()
    //Logout
    login.getUser().click()
    login.getLogout().click()

    })
    //2nd test

    it('Create Documents',function()  
    {
    const login = new loginPage()

    cy.visit(this.testdata.baseUrl);
    login.getUserName().type(this.testdata.userDocumentM)

    })
 
})

The error is

error

I have tried with above and also using before function again but same error

before(function () {
  cy.fixture('Credential').then(function (testdata) {
    this.testdata = testdata
  })
})
//2nd test

  it('Create Documents',function()  
    {
    const login = new loginPage()

    cy.visit(this.testdata.baseUrl);
    login.getUserName().type(this.testdata.userDocumentM)

  })
Paolo
  • 3,530
  • 7
  • 21
mishu
  • 11
  • 1
  • Unsure of _why_ this would be the answer, but have you tried using `beforeEach()` instead of `before()`? – agoff Dec 30 '22 at 15:23
  • You can use `import` or `require` outside of your tests to set a const variable holding the data and then use it across your tests. This is a basic example. https://stackoverflow.com/a/74964998/17917809 – jjhelguero Dec 30 '22 at 19:13

1 Answers1

2

Starting with Cypress version 12 Test Isolation was introduced. This now means the Mocha context (aka this) is completely cleaned between tests.

Mocha context

It used to be (undocumented) that the Mocha context could be used to preserve variables across tests, for example

before(function () {
  cy.fixture("example.json").as('testdata')    // preserved on "this" 
})

it("check testdata 1", function () {
  expect(this.testdata).to.not.be.undefined    // passes
})

it("check testdata 2", function () {
  expect(this.testdata).to.not.be.undefined    // fails in Cypress v12
})

but now that does not work.

The use of Mocha context is a bit arbitrary anyway, and requires explicit use of function-style functions which is easy to forget, particularly in places like array method callbacks Array.forEach(() => {}).

You can still use the Cypress context to store data

before(function () {
  cy.fixture("example").then(function (testdata) {
    Cypress.testdata = testdata;
  })
})

it("check testdata 1", function () {
  expect(Cypress.testdata).to.not.be.undefined     // passes
})

it("check testdata 2", function () {
  expect(Cypress.testdata).to.not.be.undefined     // passes
})

Note this is also undocumented and may change in the future.


Caching methods

Technically, the way to do this is to set the alias with beforeEach().

The cy.fixture() command caches it's value, so you do not get the read overhead for each test (see Fixture returns outdated/false data #4716)

There is also cy.session() for more complicated scenarios, which would be officially supported.

beforeEach(() => {
  cy.session('testdata', () => {
    cy.fixture("example").then(testdata => {
      sessionStorage.setItem("testdata", testdata)
    })
  })
})

it("check testdata 1", function () {
  expect(sessionStorage.getItem("testdata")).to.not.be.undefined
})

it("check testdata 2", function () {
  expect(sessionStorage.getItem("testdata")).to.not.be.undefined
})

Lastly, cypress-data-session which fills a gap

From the docs

Feature cy.session cy.dataSession
Command is official ✅ community
Can cache the browser session state anything
Stability experimental !!! not in v12 production
Cache across specs yes yes
Access to the cached data no ??? yes
Custom validation no yes
Custom restore no yes
Dependent caching no yes
Static utility methods limited all
GUI integration yes no
Should you use it? maybe yes
Cypress version support newer versions all

Cypress.env()

This is another way that is officially supported,

before(() => {
  cy.fixture("example").then(testdata => {
    Cypress.env("testdata", testdata)
  })
})

it("log my fixture 1", function () {
  expect(Cypress.env("testdata")).to.not.be.undefined    // passes
})

it("log my fixture 2", function () {
  expect(Cypress.env("testdata")).to.not.be.undefined    // passes
})

but there are still certain tests scenarios that reset the browser completely where this may not work.

Paolo
  • 3,530
  • 7
  • 21