Automation Testing · Playwright

Playwright Fundamentals

Playwright is a modern browser automation framework with auto-waiting, multiple browsers and excellent debugging tools.

Intermediate 5 min Lesson 9 of 24
0/24 in Automation Testing
On this page · 5 min · Intermediate

Learning objectives#

  • Install Playwright and run tests
  • Understand browsers, contexts and pages
  • Use codegen to explore locators
  • Debug with UI mode and traces

Explanation#

npm init playwright@latest scaffolds a project with config, example tests and browsers (Chromium, Firefox, WebKit).

Each test gets a fresh browser context — like a new incognito profile — so tests never share cookies or storage.

Actions auto-wait for elements to be visible, enabled and stable, removing most manual waits.

npx playwright codegen records actions into code; --ui and traces let you step through failures.

Code example: Search journey#

TypeScripttests/search.spec.ts Illustrative
import { test, expect } from '@playwright/test';


test('customer can search and open a product', async ({ page }) => {
  await page.goto('/');
  await page.getByRole('searchbox', { name: 'Search' }).fill('laptop');
  await page.keyboard.press('Enter');


  const firstResult = page.getByTestId('result').first();
  await expect(firstResult).toBeVisible();
  await firstResult.click();


  await expect(page.getByRole('heading', { level: 1 })).toContainText('Laptop');
  await expect(page.getByRole('button', { name: 'Add to cart' })).toBeEnabled();
});


// npx playwright test --project=chromium      npx playwright test --ui      npx playwright show-report
Expected output
  ✓  [chromium] › search.spec.ts:3:5 › customer can search and open a product (2.1s)
  1 passed (3.0s)

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. baseURL in the config lets page.goto("/") work.
  2. Role-based locators find elements like a user would.
  3. Each action and expect auto-waits.
  4. The HTML report shows steps, screenshots and traces.

Real-world use case#

A React team runs Playwright tests against Chromium, Firefox and WebKit on every pull request.

Common mistakes#

Adding waitForTimeout everywhere

Playwright already waits. Use web-first assertions instead.

Sharing login state by test order

Use storageState to reuse an authenticated session safely.

Best practices#

  • Set baseURL in config
  • Use role and test-id locators
  • Turn on traces for retries
  • Run tests in parallel

Remember this#

Fresh context per test.

Actions and expects auto-wait.

codegen · --ui · traces for debugging.

Practice exercise#

Your task: Run only tests whose title contains “search”.

Show solution
Shell Illustrative
npx playwright test -g "search"

Mini quiz#

Playwright Fundamentals quizMultiple choice Question 1 of 1

What does a browser context give each test?

Score so far: 0/0

Want more? Practise all Automation Testing MCQs · Automation Testing interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.