Test Overview
This test checks that a user can log in successfully using a fast login handling method. It verifies that the login process completes and the user dashboard is shown, speeding up the test suite by avoiding repeated full login flows.
This test checks that a user can log in successfully using a fast login handling method. It verifies that the login process completes and the user dashboard is shown, speeding up the test suite by avoiding repeated full login flows.
describe('Fast Login Handling Test', () => { before(() => { // Use API to log in and set session cookie cy.request('POST', '/api/login', { username: 'user1', password: 'pass123' }) .then((response) => { expect(response.status).to.eq(200); cy.setCookie('session_id', response.body.sessionId); }); }); it('should show dashboard after fast login', () => { cy.visit('/dashboard'); cy.get('h1').should('contain.text', 'Welcome, user1'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test suite starts and before hook runs | No browser opened yet; preparing login session via API | — | PASS |
| 2 | Send POST request to /api/login with username and password | API server receives login request | Response status is 200 | PASS |
| 3 | Set session cookie with sessionId from response | Browser session cookie set for authentication | — | PASS |
| 4 | Visit /dashboard page in browser | Dashboard page loads with user session active | — | PASS |
| 5 | Find <h1> element and check it contains 'Welcome, user1' | Dashboard page shows welcome message | Text contains 'Welcome, user1' | PASS |
cy.session() speed up Cypress test suites?cy.session() doescy.session() caches the login session so Cypress does not need to perform login steps repeatedly.cy.session().cy.session() correctly with a session name and callback.beforeEach(() => {
cy.session('user', () => {
cy.visit('/login')
cy.get('#username').type('user')
cy.get('#password').type('pass')
cy.get('button[type=submit]').click()
})
})
it('checks dashboard', () => {
cy.visit('/dashboard')
cy.contains('Welcome')
})cy.session() behavior in beforeEachcy.session() caches the login session so the login steps run only once, even if called in beforeEach.beforeEach(() => {
cy.session('user', () => {
cy.visit('/login')
cy.get('#username').type('user')
cy.get('#password').type('pass')
cy.get('button[type=submit]').click()
})
})
What is the likely mistake?cy.session() callback requirementscy.session callback -> Option Bcy.session() to optimize speed and reliability?before vs beforeEach hooksbefore runs once before all tests, beforeEach runs before every test.cy.session()cy.session() in before caches login once, speeding up all 10 tests reliably.cy.session() once in a before hook to cache login for all tests -> Option APass the test and this pattern moves unknown