QA Icon
QAHacks
Technical / AutomationAdvanced

How do you validate session renewal in Playwright?

📋 Interview Context

Tool Stack:Playwright

Overview

Validating session renewal in Playwright is a critical task for ensuring application resilience and user experience, often requiring a blend of UI interaction and network interception. It tests the application's ability to gracefully handle token expiration and refresh mechanisms without disrupting the user flow.

Interview Question:

How do you validate session renewal in Playwright?

Expert Answer:

Validating session renewal in Playwright requires a strategic combination of state management and network interception. The core challenge is to reliably trigger a renewal, verify the underlying API calls, and then confirm the renewed session's impact on the UI.

  1. Initial Session Establishment & State Preservation:

    • First, we establish a valid user session, typically by logging in. Playwright's context.storageState() is crucial here. After a successful login, we save the storageState to a file or fixture.
    • For subsequent tests, we can then create a new BrowserContext pre-populated with this saved state, bypassing the login UI:
      const browser = await playwright.chromium.launch();
      const context = await browser.newContext({ storageState: 'auth.json' });
      const page = await context.newPage();
      
  2. Forcing Session Expiration (Controlled Environment):

    • Short-lived Tokens: If the test environment allows, configure session tokens with a very short expiration time (e.g., 30 seconds) to easily trigger renewal.
    • Manipulating Cookies/Local Storage: Directly modify localStorage or document.cookie via page.evaluate() to expire relevant tokens, simulating an aged session without waiting. This is often the most reliable method.
      await page.evaluate(() => {
        localStorage.setItem('accessTokenExpiry', 'past_timestamp');
        // or manipulate a specific cookie
      });
      
  3. Network Interception for Renewal Verification:

    • Playwright's page.route() is indispensable for monitoring and potentially mocking the network requests involved in session renewal (e.g., /api/token/refresh).
    • We set up a route handler before triggering the action that should cause renewal.
      let refreshRequestIntercepted = false;
      await page.route('**/api/token/refresh', async route => {
        refreshRequestIntercepted = true;
        const request = route.request();
        expect(request.method()).toBe('POST');
        // Validate request payload, headers (e.g., old refresh token)
        await route.continue(); // Allow the request to proceed
      });
      
    • After route.continue(), we can wait for the response and assert its status code (e.g., 200 OK) and that a new access token is returned.
  4. UI-Driven Validation:

    • After triggering the session renewal (e.g., by navigating to a protected page, performing an action), we observe the UI.
    • Assertions include:
      • expect(page.url()).not.toContain('login'); (User was not redirected to login).
      • expect(page.locator('.protected-content')).toBeVisible(); (Protected content is still accessible).
      • No error messages related to session expiration are displayed.
  5. Post-Renewal State Verification:

    • Optionally, after a successful renewal, capture the context.storageState() again and compare it. It should contain new, updated tokens.
    • Check localStorage or cookies again to confirm new token values and updated expiry times.

Framework Integration: These steps are best encapsulated within custom Playwright fixtures or helper functions. A common pattern is to have a fixture that logs in, saves storageState, and another that sets up network routes specifically for session renewal tests, ensuring modularity and reusability within a Page Object Model (POM) structure. This robust approach ensures comprehensive validation of the renewal mechanism from both API and UI perspectives.

Speaking Blueprint (3-Minute Verbal Response):

In modern, scalable web applications, ensuring a seamless user experience through robust session management is absolutely critical, and validating session renewal is a cornerstone of our Playwright automation strategy.

Our approach centers on both state manipulation and rigorous network interception. We begin by establishing an authenticated session, saving the storageState which captures all cookies and local storage. This allows us to start subsequent tests from a pre-logged-in state, enhancing efficiency. To force a session renewal, we often use page.evaluate() to programmatically modify a specific token's expiry in local storage, simulating an expired session right before we expect a renewal to occur. This gives us precise control over when the renewal mechanism should kick in. The real power comes from Playwright's page.route(). We set up a route handler to intercept the exact API endpoint responsible for token refreshing—for instance, **/api/token/refresh. Within this handler, we assert the request method, validate the payload (ensuring the correct refresh token is sent), and confirm a successful response status code, typically 200 OK, along with the presence of a new access token in the response. Critically, after we've intercepted and validated the network flow, we then perform UI-level assertions. We verify that the user was not redirected to a login page, that protected content remains visible, and that no session-related error messages appeared. This layered validation, combining API monitoring with UI observation and state verification, provides comprehensive assurance that our session renewal process is both functionally sound and delivers a smooth, uninterrupted user experience.

This meticulous, end-to-end validation ensures not just functional correctness but significantly contributes to the application's overall resilience and security posture. It’s a key part of how we leverage Playwright to deliver high confidence in our deployments and maximize the return on investment for our automation efforts.

Continue Learning: Up Next