Skip to content Skip to sidebar Skip to footer

Passing A Same Random Number To All Tests In Cypress

So I have two tests - Test1.spec.js and Test2.spec.js and I want that with every test run a random number should be generated and the same random number should be used in both the

Solution 1:

So as per cypress docs support/index.js is run every time before each spec file is run, so my above approach is not valid because with every run a new value would be generated. So the next approach I followed was to write the values in the fixtures/data.json file on the first test and use it throughout the tests. This way with every run a new set of values would be generated and saved in the fixture file and then the same value would be used throughout the test suite for that Test Run. Below is the way how I wrote to fixtures/data.json file:

    const UniqueNumber = `${Math.floor(Math.random() * 10000000000000)}`

    cy.readFile("cypress/fixtures/data.json", (err, data) => {
        if (err) {
            return console.error(err);
        };
    }).then((data) => {
        data.username = UniqueNumber
        cy.writeFile("cypress/fixtures/data.json", JSON.stringify(data))
    })
})

Post a Comment for "Passing A Same Random Number To All Tests In Cypress"