0
0
Cypresstesting~20 mins

cy.session() for session caching in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Cypress test using cy.session()?

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?

Cypress
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');
  });
});
AloginCount is 3 because cy.login() runs once per test plus once in beforeEach
BloginCount is 2 because cy.login() runs before each test
CloginCount is 0 because cy.login() is never called
DloginCount is 1 because cy.session caches the login for both tests
Attempts:
2 left
💡 Hint

Think about how cy.session() caches the session and prevents repeated login calls.

assertion
intermediate
2:00remaining
Which assertion correctly verifies that a session is cached with cy.session()?

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?

Cypress
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
  });
});
Aexpect(loginCalls).to.equal(2);
Bexpect(loginCalls).to.equal(1);
Cexpect(loginCalls).to.be.lessThan(1);
Dexpect(loginCalls).to.be.greaterThan(1);
Attempts:
2 left
💡 Hint

Remember that cy.session() caches the session so the login function runs only once.

🔧 Debug
advanced
2:00remaining
Why does this cy.session() test fail to cache the session?

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?

Cypress
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');
  });
});
Acy.session() must be called inside each test, not beforeEach
BThe login function has a syntax error causing it to rerun
CThe session key is missing in cy.session(), so it cannot cache sessions
Dcy.visit() inside login causes session caching to fail
Attempts:
2 left
💡 Hint

Check the parameters passed to cy.session(). What is required for caching?

framework
advanced
2:00remaining
How does cy.session() improve test performance in Cypress?

Which statement best explains how cy.session() improves test speed and reliability?

AIt caches browser cookies and local storage to avoid repeated login steps across tests
BIt runs all tests in parallel to reduce total execution time
CIt disables network requests during tests to speed up execution
DIt automatically retries failed tests to improve reliability
Attempts:
2 left
💡 Hint

Think about what data cy.session() saves and restores between tests.

🧠 Conceptual
expert
2:00remaining
What happens if the session validation callback in cy.session() fails?

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?

AThe cached session is discarded and the setup callback runs again to create a new session
BThe test fails immediately with a session validation error
CThe cached session is used anyway without re-running setup
DThe test retries the validation callback indefinitely until it passes
Attempts:
2 left
💡 Hint

Consider what should happen if the cached session is no longer valid.