0
0
Cypresstesting~10 mins

cy.session() for session caching in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test logs into a web application using cy.session() to cache the login session. It verifies that the user is successfully logged in by checking the presence of a logout button.

Test Code - Cypress
Cypress
describe('Login with session caching', () => {
  beforeEach(() => {
    cy.session('user-session', () => {
      cy.visit('/login')
      cy.get('#username').type('testuser')
      cy.get('#password').type('password123')
      cy.get('#login-button').click()
      cy.get('#logout-button').should('be.visible')
    })
  })

  it('should keep user logged in using cached session', () => {
    cy.visit('/dashboard')
    cy.get('#logout-button').should('be.visible')
  })
})
Execution Trace - 10 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner is ready to execute the test suite-PASS
2cy.session() is called with key 'user-session' to cache loginNo cached session exists yet-PASS
3Browser opens and navigates to '/login'Login page is displayed with username, password fields and login button-PASS
4Finds username input field and types 'testuser'Username field contains 'testuser'-PASS
5Finds password input field and types 'password123'Password field contains 'password123'-PASS
6Finds and clicks the login buttonLogin form is submitted-PASS
7Checks that logout button is visible to confirm login successDashboard page or logged-in state with logout button visibleAssert logout button is visiblePASS
8Session 'user-session' is cached for reuseSession data stored internally by Cypress-PASS
9Test visits '/dashboard' page using cached sessionDashboard page loads without needing to login again-PASS
10Checks that logout button is visible on dashboardUser is confirmed logged in on dashboardAssert logout button is visiblePASS
Failure Scenario
Failing Condition: Login fails or logout button is not found after login attempt
Execution Trace Quiz - 3 Questions
Test your understanding
What does cy.session() do in this test?
AClears cookies before each test
BReloads the page after login
CCaches the login session to reuse it in later tests
DClicks the logout button automatically
Key Result
Using cy.session() improves test speed and reliability by caching login state, avoiding repeated login steps in each test.