I want to implement BDD with Cypress and want to retain same session id within different step files.
So i have different step files for different feature files.
My feature files look like
1.Login-Login to application
2.Add Employee-Add employee after logging in.
I want to use the same login credentials in step definitions for feature file.
But the problem is cypress clears all cookies within different tests.
So how to achieve this dependency.
There is a Cypress plug-in cypress-localstorage-commands
you can use to preserve the value of a variable, which can then be used across tests.
You can use it like this:
let SESSION_ID = '';
before(() => {
cy.setLocalStorage('sessionId', '000'); // capture the id for example
cy.saveLocalStorage()
});
beforeEach(() => {
cy.restoreLocalStorage();
cy.getLocalStorage('sessionId').as('id');
SESSION_ID = id;
});
it('test 1', () => {
cy.get(SESSION_ID).should('eq', '000');
});
it('test 2', () => {
cy.log(SESSION_ID);
});
Reference: https://www.npmjs.com/package/cypress-localstorage-commands
The other option is maybe use the fixture files and the command cy.writeFile
to save the session id under the before
and then capture it under your tests it