Consider the following Cypress test code that uses cy.session() to cache a login session. What will be the value of loginCount after running both tests?
let loginCount = 0; Cypress.Commands.add('login', () => { loginCount++; cy.visit('/login'); cy.get('#username').type('user'); cy.get('#password').type('pass'); cy.get('button[type=submit]').click(); }); describe('Session caching test', () => { beforeEach(() => { cy.session('user-session', () => { cy.login(); }); }); it('Test 1', () => { cy.visit('/dashboard'); }); it('Test 2', () => { cy.visit('/profile'); }); });
Think about how cy.session() caches the session and prevents repeated login calls.
The cy.session() command caches the session after the first login call. So, cy.login() runs only once, even though there are two tests. Therefore, loginCount is 1.
You want to verify that the session caching works by checking that the login function is called only once. Which assertion below correctly tests this behavior?
let loginCalls = 0; Cypress.Commands.add('login', () => { loginCalls++; // login steps }); describe('Session caching', () => { beforeEach(() => { cy.session('session-key', () => { cy.login(); }); }); it('Test A', () => { cy.visit('/pageA'); }); it('Test B', () => { cy.visit('/pageB'); }); after(() => { // Assertion here }); });
Remember that cy.session() caches the session so the login function runs only once.
The login function is called only once because cy.session() caches the session. So the correct assertion is expect(loginCalls).to.equal(1);.
Look at the code below. The session caching does not work as expected and the login function runs before every test. What is the most likely cause?
let loginCount = 0; Cypress.Commands.add('login', () => { loginCount++; cy.visit('/login'); cy.get('#user').type('user'); cy.get('#pass').type('pass'); cy.get('button').click(); }); describe('Session caching failure', () => { beforeEach(() => { cy.session(() => { cy.login(); }); }); it('Test 1', () => { cy.visit('/home'); }); it('Test 2', () => { cy.visit('/settings'); }); });
Check the parameters passed to cy.session(). What is required for caching?
cy.session() requires a unique session key as the first argument to identify and cache the session. Without the key, it cannot cache and will rerun the login each time.
Which statement best explains how cy.session() improves test speed and reliability?
Think about what data cy.session() saves and restores between tests.
cy.session() caches authentication data like cookies and local storage. This avoids repeating login steps, making tests faster and more stable.
In cy.session(), you can provide a validation callback to check if the cached session is still valid. What is the behavior if this validation callback returns false or throws an error?
Consider what should happen if the cached session is no longer valid.
If the validation callback fails, cy.session() discards the cached session and runs the setup callback again to create a fresh session. This ensures tests run with valid sessions.