Manuals / Cypress / Chapter 6

B · Core · intermediate · Week 4 · Chapter 6 of 12

cy.session — fast auth without re-login

Logging in every test wastes minutes in CI. cy.session caches authenticated state across specs while keeping tests isolated.

Path progress
44%

Step 1 of 3

Basic session

cy.session([user, pass], () => { ... login steps ... }) runs login once, restores cookies/localStorage after.

Basic sessionDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Basic sessiondrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

cy.session([username, password], () => {
  cy.visit("/");
  cy.get("[data-test=username]").type(username);
  cy.get("[data-test=password]").type(password);
  cy.get("[data-test=login-button]").click();
});

Do this now

Wrap login in cy.session. Run suite — second spec should skip UI login.

Was this step clear?
Chapter learning outcomes
  • Session cache
  • validate callback
  • Multi-user sessions

Clear these before you leave

Side quest

Session + intercept combo

Session login + intercept inventory — full pattern for CI speed.