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

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)
})
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
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
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.Lastly, cypress-data-session which fills a gap
From the docs
experimental!!! not in v12Cypress.env()
This is another way that is officially supported,
but there are still certain tests scenarios that reset the browser completely where this may not work.