Developer Guides

Playwright Tutorial: Setup and Example from Scratch

Learn Playwright from scratch with a step-by-step tutorial covering installation, basic setup, and running an example test.

4 min read

Automation testing has surfaced as a critical component in modern software development. Playwright—an open-source browser automation framework—is increasingly becoming the tool of choice for its robust cross-browser capabilities. In this tutorial, we will guide you to learn Playwright from scratch, including installation, setup, and running your first test.


Prerequisites

To start playing with Playwright, make sure your setup satisfies the following requirements:

prerequisites

  • Node.js: Install Node.js (recommended LTS version) from Node.js official website.
  • Code Editor: Set up your IDE, ideally Visual Studio Code.
  • VS Code Playwright Extension (Optional): Install the Playwright plugin for VS Code to streamline test writing.

Installation Steps for Playwright

Installing Playwright is straightforward. Follow the steps below to set it up.

steps

  1. Open your terminal (or your terminal in VS Code).
  2. Initialize a new Playwright project by running:
    bash
    npm init playwright@latest
  3. During the setup wizard:
    • Choose between JavaScript or TypeScript for the language.
    • Select the browsers to install (Chromium, Firefox, WebKit). By default, all three will be selected.

Creating Your First Test

Once your Playwright setup is complete, you can easily create and run your first test.

steps

  1. Use the automatically generated example test:
    • Navigate to the tests directory created by Playwright.
  2. Run the following command to execute your test:
    bash
    npx playwright test
  3. Observe the outcome to verify the test success or failure.
  4. To see the execution visually (headed mode), use:
    bash
    npx playwright test --headed

Understanding Playwright Basics

Playwright allows developers to automate web application testing across multiple platforms. Here are some foundational concepts to understand:

  1. Multi-Browser Support: Automate major browsers like Chromium, Firefox, and WebKit.
  2. Languages: Playwright supports JavaScript, TypeScript, Python, Java, and .NET.
  3. Cross-Platform Execution: Run tests on Windows, macOS, and Linux, including local machines and CI/CD pipelines.
  4. Built-In Features: Auto-waiting, tracing capabilities, and built-in assertions simplify the testing process.

Adding Assertions

Playwright comes equipped with flexible assertion capabilities to validate test outcomes.

steps

  1. Use the expect function to assert UI elements. For example:
    javascript
    const { test, expect } = require('@playwright/test');
    
    test('Validate Title', async ({ page }) => {
      await page.goto('https://example.com');
      await expect(page).toHaveTitle('Example Domain');
    });
  2. Use assertions to validate key UI components, such as buttons, headers, or URLs.

Running Tests in Headed and Headless Modes

Switching between headed and headless execution is seamless with Playwright.

Run Test in Headless Mode

npx playwright test
# Default mode - Runs tests without showing the browser UI.

Run Test in Headed Mode

npx playwright test --headed
# Runs tests with a visible browser window.

Debugging and Reporting

In case of errors, Playwright offers debugging and reporting tools to help analyze test failures.

Debugging

  • Use Playwright’s Trace Viewer to visualize detailed test execution steps.
  • To enable tracing:
    javascript
    const { test } = require('@playwright/test');
    
    test('Enable tracing', async ({ context, page }) => {
      await context.tracing.start({ screenshots: true, snapshots: true });
      await page.goto('https://example.com');
      await context.tracing.stop({ path: 'trace.zip' });
    });

Reporting

  • Configure Playwright to generate reports, such as HTML reports.
  • Default HTML reporting can be viewed using:
    bash
    npx playwright show-report

Tips for Maintaining Playwright Tests

Maintaining a test suite can be challenging, but these tips can help:



FAQ

How do I run multiple Playwright tests in a single file?

To run multiple tests in the same file, write them as test blocks using the test() function exported by Playwright. All valid test definitions will be executed when you run the file.

Can Playwright record test scripts automatically?

Yes, Playwright offers a feature called Codegen for automatic test generation. Run npx playwright codegen to start recording your automation steps.

What browsers does Playwright support?

Playwright supports Chromium, Firefox, and WebKit engines, enabling cross-browser testing for Chrome, Edge, Safari, and Firefox.

How can I debug a failing Playwright test?

Playwright provides a Debug Mode to visually observe test execution. Use the --headed flag for visible execution or configure trace capturing using the tracing feature for in-depth debugging.