https://cypress.io logo
How to keep a session logged.
# i-need-help
p
So basically have been trying to search for and basically fail (note: I am a beginner), to run tests where they would run in a series, at the moment after every test I am getting logged out.
e
Try using a
before()
with your login and
cy.storeLocalStorage()
, and then within a
beforeEach()
try using
cy.restoreLocalStorage()
p
I see so if I were to have 2 tests and sorry i'm still learning
describe("Login", () => {
before()
cy.storeLocalStorage()
it("tests Login", () => {
cy.viewport(1920, 1080);
cy.visit("https://staging.pabau.com/");
cy.get("#email").click();
cy.get("#email").type("automation@automation.com");
cy.get("#password").click();
cy.get("#password").type("Automation123@");
cy.get("button").click();
});
});
?
e
I'd take your login commands and abstract them in a reusable Custom Command. Then I'd wrap your login commands with
cy.session
which will automatically persist all the storage and cookie data for the session. Maybe read up on this command and look at their examples? https://docs.cypress.io/api/commands/session
g
I teach how to stay logged in or restore session in my paid course https://cypress.tips/courses/swag-store
p
Amazing, thank you @enough-truck-68085
e
Sure thing, let me know if you are having trouble after implementation.
p
Yeah, I'm actually a complete beginner and trying to learn on how it works by recording via a plugin and then disecting the code down
e
cy.session
definitely builds on several other concepts and commands first. If you are completely new to Cypress and how it works I'd start from the beginning of their documentation to break it down even more before diving into the deep end
p
I technically have another question, if I wanted to run multiple tests but in 1 file then I'd create a e.g 'one-run.cy.js' and then include all of my code in it would that work?
As, I can record tests without a problem, just all of the tests would log me out as soon as they start and one finishes.
e
You can have as many tests in a single file that you want but they would only execute in order, one at a time. Having multiple tests is a good strategy when you are grouping like-tests. It also allows you share setup across multiple tests through
before
and
beforeEach
hooks. There's definitely a balance because you don't want to have a mega-spec either where it's too hard to read and everything is jammed into a single file. Use best judgement when organizing, writing, and reading tests so that they scale appropriately.
p
What are your thoughts on tools that generate cy test-cases based on recording? If i were to record multiple and then use them I'd need to figure out how to execute the command about login.
e
I've never used tools like that and would rather learn and write the code myself. I also don't fully trust tools like that and wouldn't have full confidence on the results they provide. For the sake of everyone else in this chat, let's keep any further conversation to the original issue around login in this chat, please. If you have other technical or cypress questions I'm happy to help on the side and you can reach out to me. 🙂
p
Fair enough and thank you
e
FWIW a lot of those tools are using CSS and XPATH selectors which may work fine for a bit, but when UI starts changing it can kill your tests. So it's better to start using things like
data-testid
for elements that way when devs start adjusting the frontend those
data-testid
attributes wont change.
p
Cheers all for the help!