QA Graphic

Speed Up Your Tests with storageState()

Little Known Tip to Get Your Test Running Faster

Tired of logging in before every test? Playwright’s storageState() lets you save your login state and reuse it across multiple tests - boosting performance and reliability.

Quick Tip: Reusing browser context cuts down execution time and enhances CI/CD pipeline efficiency.

Why storageState() Matters

Every time your test logs in, you're spending time and creating potential points of failure. Also it adds additional overhead on your tests - that may impact the overall test time. Instead, you can log in once, save the state (cookies + local storage), and load that state in future tests.

Step 1: Save the Auth State

Create a setup script that logs in and saves the storage state.

// setup-auth.ts
import { chromium } from '@playwright/test';
(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://example.com/login');
  await page.fill('#username', 'your-username');
  await page.fill('#password', 'your-password');
  await page.click('button[type="submit"]');
  // Wait for redirect or element that confirms login
  await page.waitForSelector('#dashboard');
  // Save storage state to file
  await context.storageState({ path: 'auth.json' });
  await browser.close();
})();

Step 2: Reuse the Auth State

In your test file, load the previously saved auth.json file to reuse the session.

// example.spec.ts
import { test, expect } from '@playwright/test';
test.use({
  storageState: 'auth.json'
});
test('Dashboard loads for authenticated user', async ({ page }) => {
  await page.goto('https://example.com/dashboard');
  await expect(page.locator('#dashboard')).toBeVisible();
});

When Should You Use This?

  • Tests that require an authenticated user
  • Reducing login redundancy in CI/CD pipelines
  • Speeding up test suites with shared session state
Pro Tip: You can use different storage files for different roles or test scenarios.

Start using storageState() today and take the fast lane through your E2E tests!

 

Comments

Add Your Comments

Name:
Comment:

 

About

Welcome to Playwright Tips and Tricks, your go-to resource for mastering the art of web automation and testing with Playwright! Whether you're a seasoned developer looking to streamline your workflows or a curious beginner eager to dive into the world of browser automation, this blog is designed with you in mind. Here, I'll share a treasure trove of practical insights, clever hacks, and step-by-step guides to help you harness the full power of Playwright - a modern, open-source tool that's revolutionizing how we interact with web applications.

Schedule

Monday 12 Media
Tuesday 13 QA
Wednesday 14 Pytest
Thursday 15 PlayWright
Friday 16 Macintosh
Saturday 17 Internet Tools
Sunday 18 Misc