Skip to main content
Hands-On Code Drills • TypeScript

Playwright Automation Practice Test

Sharpen your modern browser automation skills with 4 production-grade Playwright drills. Master user-facing locators, deterministic auto-waiting, network mocking with page.route(), and CI failure triage.

Author:Shivam Singh (Founder)
Language:TypeScript • Node.js
Framework:Playwright Test Runner
Updated:October 2026 Edition

Drill 1: User-Facing Locators & Auto-Waiting Mechanics

Scenario: Automate a user login flow targeting accessible roles instead of brittle CSS selectors or absolute XPaths.

PlaywrightTest.spec.tsTypeScript
import { test, expect } from '@playwright/test';

test('automate login using accessible role locators', async ({ page }) => {
  await page.goto('https://www.testerrank.com/banking/login');

  // Playwright automatically waits for elements to be visible and enabled
  await page.getByLabel('Username or Account ID').fill('demo_user_88');
  await page.getByLabel('Password').fill('SecurePassword123!');

  // Target the button by its accessible role and text
  await page.getByRole('button', { name: 'Sign In' }).click();

  // Assert expected post-login dashboard state
  await expect(page.getByRole('heading', { name: 'Account Overview' })).toBeVisible();
  await expect(page).toHaveURL(/.*dashboard/);
});

Playwright eliminates explicit waits for standard actions. Methods like `.click()` and `.fill()` perform actionability checks (visible, stable, enabled, editable) before executing. Preferring `getByRole` and `getByLabel` ensures your automation mirrors assistive technology and real user behavior.

Drill 2: Network Interception & API Response Mocking

Scenario: Mock a backend 500 error or simulate empty account balances without altering database records.

PlaywrightTest.spec.tsTypeScript
import { test, expect } from '@playwright/test';

test('simulate backend 500 error on transfer flow', async ({ page }) => {
  // Intercept backend API call before navigating
  await page.route('**/api/v1/transfers', async (route) => {
    await route.fulfill({
      status: 500,
      contentType: 'application/json',
      body: JSON.stringify({ error: 'Core Banking Gateway Timeout' }),
    });
  });

  await page.goto('https://www.testerrank.com/banking/transfer');
  await page.getByRole('button', { name: 'Confirm Transfer' }).click();

  // Verify graceful error banner displayed in UI
  const errorAlert = page.getByRole('alert');
  await expect(errorAlert).toContainText('Core Banking Gateway Timeout');
});

`page.route()` intercepts browser network requests at the engine level. This allows SDETs to test edge cases (500 internal errors, 429 rate limits, empty states) deterministically without relying on third-party service stability.

Drill 3: Handling Popups, Dialogs & Multiple Browser Tabs

Scenario: Handle dynamic browser alert dialogs and capture child window popups in concurrent test execution.

PlaywrightTest.spec.tsTypeScript
import { test, expect } from '@playwright/test';

test('handle new window popup cleanly', async ({ context, page }) => {
  await page.goto('https://www.testerrank.com/topics/windows-tabs');

  // Listen for the popup event concurrently with the click action
  const [newPage] = await Promise.all([
    context.waitForEvent('page'),
    page.getByRole('button', { name: 'Open New Window' }).click(),
  ]);

  await newPage.waitForLoadState('domcontentloaded');
  await expect(newPage).toHaveTitle(/Verification/);

  // Close child tab and continue work on parent tab
  await newPage.close();
  await expect(page.getByRole('button', { name: 'Open New Window' })).toBeEnabled();
});

Unlike Selenium which requires manual handle switching (`driver.switchTo().window()`), Playwright uses the event-driven `waitForEvent('page')` pattern within `Promise.all` to avoid race conditions when tabs open rapidly.

Drill 4: CI Failure Triage with Trace Viewer & Retries

Scenario: Configure Playwright config for CI/CD runs to capture step-by-step DOM snapshots and video on first retry.

PlaywrightTest.spec.tsTypeScript
// playwright.config.ts configuration snippet
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 2 : undefined,
  reporter: [['html'], ['list']],
  use: {
    baseURL: 'https://www.testerrank.com',
    trace: 'on-first-retry', // Captures DOM snapshots and network log on failure
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
  },
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox', use: { ...devices['Desktop Firefox'] } },
  ],
});

`trace: 'on-first-retry'` is the gold standard for CI pipelines. It records zero trace overhead for passing tests, but if a test fails in GitHub Actions or Jenkins, Playwright re-runs it with full DOM inspection, screencasts, console logs, and network waterfalls.

Interactive Test Sandboxes

Run Playwright tests against live portals

Target forms, sliders, dynamic tables, and authentication modals across our 9 free practice portals.