4 Hours

7. Playwright: End-to-End Testing Fundamentals

Playwright

Playwright is a powerful end-to-end testing framework for web applications. It allows you to test your application as a real user would, interacting with the UI and verifying behavior across multiple browsers. Understanding Playwright is essential for building reliable web applications.

The key insight is that Playwright tests are user-centric. They simulate real user interactions like clicking, typing, and navigating. Playwright tests run in actual browsers, catching issues that unit tests might miss. Understanding Playwright is essential for complete test coverage.

This module covers Playwright setup, basic tests, locators, and assertions.

Playwright Setup and Basic Tests

Playwright is installed via npm. It provides a test runner, browser automation, and assertions. Playwright tests are written in JavaScript or TypeScript. The basic structure: import test and expect, write a test function, perform actions, and make assertions.

Playwright supports multiple browsers: Chromium, Firefox, and WebKit. You can run tests in all three browsers to ensure cross-browser compatibility. Playwright also supports headless and headed modes for debugging.

Locators and Assertions

Playwright locators are used to find elements on the page. They support: text content, CSS selectors, role, label, placeholder, and more. Playwright also supports chaining locators for complex queries.

Playwright's assertions are async and auto-retry. This means they wait for the condition to be true, reducing flaky tests. Common assertions: toBeVisible, toHaveText, toHaveValue, and toBeDisabled. Using Playwright locators and assertions correctly is essential for reliable tests.

JavaScript
// test.spec.js
import { test, expect } from '@playwright/test';

test.describe('Login Page', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('https://example.com/login');
  });

  test('should show login form', async ({ page }) => {
    await expect(page.locator('h2')).toHaveText('Login');
    await expect(page.locator('input[type="email"]')).toBeVisible();
    await expect(page.locator('input[type="password"]')).toBeVisible();
    await expect(page.locator('button[type="submit"]')).toBeVisible();
  });

  test('should login with valid credentials', async ({ page }) => {
    await page.fill('input[type="email"]', 'user@example.com');
    await page.fill('input[type="password"]', 'password123');
    await page.click('button[type="submit"]');
    
    await expect(page).toHaveURL('https://example.com/dashboard');
    await expect(page.locator('h1')).toHaveText('Welcome, User!');
  });

  test('should show error with invalid credentials', async ({ page }) => {
    await page.fill('input[type="email"]', 'invalid@example.com');
    await page.fill('input[type="password"]', 'wrongpassword');
    await page.click('button[type="submit"]');
    
    await expect(page.locator('.error')).toBeVisible();
    await expect(page.locator('.error')).toHaveText('Invalid credentials');
  });
});

Testing Interactions

Playwright supports a wide range of user interactions: click, fill, selectOption, check, uncheck, hover, and more. These interactions simulate real user behavior, making your tests reliable and realistic. Playwright also supports keyboard events, mouse events, and file uploads.

Interactions are async and automatically wait for the element to be ready. This reduces flaky tests caused by timing issues. Use the appropriate interaction method for each user action.

JavaScript
// Interaction examples
test('user interactions', async ({ page }) => {
  await page.goto('https://example.com');
  
  // Fill a text input
  await page.fill('#search', 'Playwright');
  
  // Click a button
  await page.click('#search-button');
  
  // Select an option from a dropdown
  await page.selectOption('#country', 'US');
  
  // Check a checkbox
  await page.check('#agree');
  
  // Upload a file
  await page.setInputFiles('#file-upload', 'path/to/file.txt');
  
  // Hover over an element
  await page.hover('#dropdown-menu');
  
  // Press a key
  await page.keyboard.press('Enter');
});

Try It Yourself: Writing Playwright Tests

Follow these steps to write your first Playwright tests.

Step 1: Install Playwright
npm init playwright@latest<br>npm install

Step 2: Create a test file
Create tests/example.spec.js:
import { test, expect } from <strong><code>@playwright/test;

test(has title, async ({ page }) => {
await page.goto(https://playwright.dev/);
await expect(page).toHaveTitle(/Playwright/);
});

test(navigates to docs, async ({ page }) => {
await page.goto(https://playwright.dev/);
await page.click(text=Get started);
await expect(page).toHaveURL(/.*intro/);
});

Step 3: Run the tests
npx playwright test

Step 4: View the report
npx playwright show-report

Try It Yourself: Write tests for a real application you're building. Test the login flow, navigation, and key user interactions.

Knowledge Check

Ready to test your understanding of 7. Playwright: End-to-End Testing Fundamentals?

Back