0
0
Cypresstesting~20 mins

Preserving state between tests in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Preservation 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 code?

Consider the following Cypress test code that uses cy.session() to preserve login state between tests. What will be the value of loggedIn after both tests run?

Cypress
let loggedIn = false;

describe('Session preservation', () => {
  before(() => {
    cy.session('user-session', () => {
      cy.visit('/login');
      cy.get('#username').type('user');
      cy.get('#password').type('pass');
      cy.get('#login-button').click();
    });
  });

  it('Test 1: sets loggedIn to true', () => {
    cy.visit('/dashboard');
    cy.get('h1').should('contain', 'Dashboard');
    loggedIn = true;
  });

  it('Test 2: checks loggedIn value', () => {
    expect(loggedIn).to.equal(true);
  });
});
AThe second test fails with a ReferenceError for loggedIn.
BThe second test fails because loggedIn is false.
CThe second test passes because loggedIn is true.
DThe second test passes but loggedIn is undefined.
Attempts:
2 left
💡 Hint

cy.session() preserves browser session state, but JavaScript variables declared outside tests persist across it blocks automatically.

assertion
intermediate
1:30remaining
Which assertion correctly verifies that a cookie is preserved between tests?

You want to check that the cookie named auth_token is preserved between two Cypress tests. Which assertion inside the second test will correctly verify this?

Aexpect(cy.getCookie('auth_token')).to.have.property('value');
Bexpect(cy.getCookie('auth_token')).to.exist;
Ccy.getCookie('auth_token').should('have.value');
Dcy.getCookie('auth_token').should('exist');
Attempts:
2 left
💡 Hint

Remember that cy.getCookie() returns a Cypress chainable, so assertions should be chained.

🔧 Debug
advanced
2:30remaining
Why does this Cypress test fail to preserve localStorage between tests?

Look at this Cypress test suite. The localStorage is set in the first test but is empty in the second test. Why?

Cypress
describe('LocalStorage test', () => {
  it('sets localStorage item', () => {
    cy.visit('/');
    cy.window().then((win) => {
      win.localStorage.setItem('token', 'abc123');
    });
    cy.get('#status').should('contain', 'Logged in');
  });

  it('checks localStorage item', () => {
    cy.visit('/');
    cy.window().then((win) => {
      const token = win.localStorage.getItem('token');
      expect(token).to.equal('abc123');
    });
  });
});
AlocalStorage is cleared between tests by Cypress, so the second test sees empty localStorage.
BlocalStorage.setItem() is asynchronous and not awaited, so the item is not saved.
ClocalStorage is not accessible in Cypress tests due to security restrictions.
DThe second test runs before the first test, so localStorage is empty.
Attempts:
2 left
💡 Hint

Think about how Cypress resets the browser state between tests.

framework
advanced
1:30remaining
Which Cypress command is designed to preserve state like cookies and localStorage between tests?

To avoid logging in before every test, which Cypress command helps preserve session state automatically?

Acy.preserve()
Bcy.session()
Ccy.cache()
Dcy.keepState()
Attempts:
2 left
💡 Hint

Check Cypress documentation for session management commands.

🧠 Conceptual
expert
2:00remaining
What is the main reason Cypress isolates state between tests by default?

Why does Cypress clear cookies, localStorage, and sessionStorage between tests unless explicitly preserved?

ATo ensure tests are independent and do not affect each other, improving reliability.
BTo speed up test execution by clearing browser data.
CBecause Cypress cannot access browser storage across tests due to security policies.
DTo force developers to write longer tests that include login steps every time.
Attempts:
2 left
💡 Hint

Think about test best practices and why isolation matters.