DEV Community

Shell QA
Shell QA

Posted on

Accelerating UI Tests with Session Management and Login Bypass in Playwright

APP Login Bypass - Session Management

Overview

The APP framework now includes automatic login bypass using session management. After the first successful login, the authentication state is saved and reused for subsequent test runs, significantly reducing test execution time.

How It Works

First Run (Full Login)

  • Test navigates to APP login page.

  • Enters credentials and logs in.

  • After successful authentication, the session state is automatically saved.

  • Test continues normally.

Subsequent Runs (Bypass Login)

  • Framework checks for valid saved session.

  • If session exists and is valid (< 2 hours old):

    • ✅ Login is bypassed
    • Test goes directly to APP home
    • Saves 5–10 seconds per test
  • If session expired or invalid:

    • Performs normal login
    • Saves new session

Benefits

  • Faster Test Execution: Skip login for every test after the first one.

  • Reduced APP Load: Fewer authentication requests.

  • Better Test Stability: Less network dependency during test runs.

  • Automatic Management: No configuration needed.

Session Storage

Location

Sessions are stored in .auth-sessions/

  • .auth-session.json - APP sessions

Session Lifetime

  • Valid for: 2 hours from last save.

  • Auto-refresh: Session is updated after each successful login.

  • Auto-cleanup: Expired sessions are automatically cleared.

Usage

No changes needed in your tests! The bypass works automatically.

Example Test Flow

Given I login to APP application with Super User 'User1'
Enter fullscreen mode Exit fullscreen mode
First run:
 * ℹ️ No saved session found
 * 🔑 Logging in with credentials...
 * ✅ Login successful!
 * 💾 Session saved for APP
 * ⏱️ Time: ~8 seconds
Enter fullscreen mode Exit fullscreen mode
Second run (within 2 hours):
 * 🔍 Looking saved session for APP
 * 🟢 Valid session found (age: 15 minutes)
 * ⚡ Bypassing login using saved session...
 * ✅ Login bypassed successfully for APP
 * ⏱️ Time: ~2 second
Enter fullscreen mode Exit fullscreen mode

Manual Session Management

Clear Sessions (Force Fresh Login)

// Clear APP session
node -e "require('./utils/SessionManager.js').default.clearSession()"

// Clear all sessions
node -e "require('./utils/SessionManager.js').default.clearAllSessions()"
Enter fullscreen mode Exit fullscreen mode

Or simply delete the session files:

# Windows
Remove-Item .auth-sessions/* -Force

# Linux/Mac
rm -rf .auth-sessions/*
Enter fullscreen mode Exit fullscreen mode

When Bypass Occurs

✅ Bypass will work when:
  • Valid session exists (< 2 hours old)

  • APP session is still active

  • Same environment

  • No APP logout occurred

❌ Bypass will NOT work and full login occurs when:
  • First test run (no session exists)

  • Session older than 2 hours

  • APP session expired on server

  • Different user/environment

  • Session file corrupted or deleted

Security

Session Files

  • Stored locally in .auth-sessions/

  • Added to .gitignore - never committed to git

  • Contains authentication tokens and cookies

  • Automatically cleared up when expired

Best Practices

  • Never commit .auth-sessions/ to version control.

  • Clear sessions before switching environments.

  • Let sessions expire naturally for better security.

  • Use environment variables for credentials (never hardcode).

Troubleshooting

Issue: Login bypass not working

  • Solution:
   # Clear sessions and try again
     Remove-Item .auth-sessions/* -Force
     npm test
Enter fullscreen mode Exit fullscreen mode

Issue: "Session expired" message

  • Cause: Session older than 2 hours or APP invalidated it.

  • Solution: Framework will automatically perform full login and save new session.

Issue: Tests fail after bypass

  • Solution:

    • Clear the session:
     Remove-Item .auth-sessions/* -Force
Enter fullscreen mode Exit fullscreen mode
  • Run tests again to create fresh session.

Configuration

Change Session Lifetime

Edit utils/SessionManager.js:

const maxAge = 2 * 60 * 60 * 1000; // Default: 2 hours

// Change to 1 hour:
const maxAge = 1 * 60 * 60 * 1000;

// Change to 30 minutes:
const maxAge = 30 * 60 * 1000;
Enter fullscreen mode Exit fullscreen mode

Disable Session Bypass

To disable bypass and always perform full login:

 // In step definition, comment out bypass check:
 // const bypassSuccessful = await SessionManager.loginWithBypass(this.page, 'app');
 // if (bypassSuccessful) return;
Enter fullscreen mode Exit fullscreen mode

Performance Impact

Time Savings

Scenario Without Bypass With Bypass Time Saved
Single test ~8s login ~2s bypass 6 seconds
5 APP tests ~40s login ~10s bypass 30 seconds
10 APP tests ~80s login ~20s bypass 60 seconds
Full suite (20 tests) ~160s login ~40s bypass 120 seconds

Average savings: ~75% reduction in login time.

Technical Details

Session Data Stored

  • Cookies (including authentication tokens)

  • Local storage

  • Session storage

  • Origin data

  • Timestamp of session save

Session Validation

  • Check if session file exists.

  • Verify session age (< 2 hours).

  • Load session state into browser context.

  • Navigate to APP.

  • Verify UI is loaded.

  • If verification fails, fall back to full login.

CI/CD Considerations

GitHub Actions / Jenkins

Sessions will not persist between CI/CD runs (each run starts fresh). This is by design for security.

Parallel Execution

Each parallel worker gets its own browser context but can share the same session file. This works because:

  • Session file is read-only during tests.

  • Only updated after successful login.

  • File system handles concurrent reads.

Summary

  • Automatic - No code changes needed

  • 🚀 Fast - ~75% faster login times

  • 🔒 Secure - Sessions expire and are never committed

  • 🛡️ Reliable - Falls back to full login if session invalid

  • 📦 Separate - APP sessions managed independently

Your APP tests just got faster! 🚀

Top comments (1)

Collapse
 
locitra profile image
Sunil Kumar Uikey

The session-management approach is particularly useful for keeping UI test suites fast without sacrificing coverage. Repeatedly going through authentication flows can make browser tests unnecessarily slow and also introduce another source of flaky failures.

I like the idea of separating the authentication setup from the actual test scenarios. It makes the suite easier to reason about and should become even more valuable as applications add more complex login and permission flows.