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.
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.
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') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner is ready to execute the test suite | — | PASS |
| 2 | cy.session() is called with key 'user-session' to cache login | No cached session exists yet | — | PASS |
| 3 | Browser opens and navigates to '/login' | Login page is displayed with username, password fields and login button | — | PASS |
| 4 | Finds username input field and types 'testuser' | Username field contains 'testuser' | — | PASS |
| 5 | Finds password input field and types 'password123' | Password field contains 'password123' | — | PASS |
| 6 | Finds and clicks the login button | Login form is submitted | — | PASS |
| 7 | Checks that logout button is visible to confirm login success | Dashboard page or logged-in state with logout button visible | Assert logout button is visible | PASS |
| 8 | Session 'user-session' is cached for reuse | Session data stored internally by Cypress | — | PASS |
| 9 | Test visits '/dashboard' page using cached session | Dashboard page loads without needing to login again | — | PASS |
| 10 | Checks that logout button is visible on dashboard | User is confirmed logged in on dashboard | Assert logout button is visible | PASS |
cy.session() in Cypress tests?cy.session() doescy.session() caches login or setup steps so tests don't repeat them.cy.session() saves login to speed tests [OK]cy.session()?cy.session() syntax() => { cy.login() }.cy.session('user', () => { cy.login() }) correctly passes a callback function. The other options pass a function reference incorrectly, execute the function immediately, or use invalid syntax.cy.session('admin', () => {
cy.visit('/login')
cy.get('#username').type('admin')
cy.get('#password').type('password')
cy.get('button[type=submit]').click()
})
cy.session('admin', () => {
cy.visit('/dashboard')
})cy.session() callcy.session():cy.session('user', () => {
cy.visit('/login')
cy.get('#user').type('test')
cy.get('#pass').type('1234')
cy.get('button').click()
})
cy.session('user', () => {
cy.visit('/profile')
})cy.session() usage to fix this?validate callback in cy.session()validate callback runs before reusing a session to confirm it is still valid, forcing a fresh login if needed.cy.session() slows tests. Clearing cookies manually is less efficient.validate callback to check session validity before reuse -> Option BPass the test and this pattern moves unknown