No history yet

Introduction to Playwright

What is Playwright?

Playwright is a tool for automating web browsers. Developed by Microsoft, it's an open-source framework designed for end-to-end testing of web applications. Think of it as a robot that can act like a real user: clicking buttons, filling out forms, and navigating through pages to ensure everything works as expected.

Playwright is an an open source, end to end testing framework by Microsoft for testing your modern web applications.

One of its biggest strengths is cross-browser support. It can run tests on Chromium (the engine behind Google Chrome and Microsoft Edge), Firefox, and WebKit (the engine for Apple's Safari). This means you can write one test and confirm your application works correctly for a wide range of users, no matter which browser they use. It also works across different operating systems like Windows, Linux, and macOS.

Playwright isn't just for checking what users see on the screen (the UI). It can also test the underlying Application Programming Interfaces (APIs), giving you a complete picture of your application's health.

Why Choose Playwright?

Several tools exist for web testing, but Playwright stands out for a few key reasons. It's known for being fast, reliable, and packed with useful features.

The Playwright testing framework is more straightforward and lightweight than most alternatives.

Reliability is a major challenge in automated testing. Tests can sometimes fail not because the application is broken, but because a web element took a little longer than usual to load. These are called "flaky" tests, and they can be a huge headache. Playwright solves this with a clever feature called auto-waiting.

As Playwright is like a really fast user, it waits for elements to be actionable before it clicks on them and retries when making assertions to ensure your tests are not flaky.

This means Playwright automatically waits for buttons to be clickable or for text to appear before it tries to interact with them. This simple but powerful feature makes tests much more stable and trustworthy.

Playwright also comes with a rich set of features out of the box. It can emulate different devices, so you can see how your site behaves on a mobile phone versus a desktop. It can also generate tests for you by simply recording your actions in the browser, which is a great way to get started.

Getting Started

Setting up a Playwright project is simple. You'll need Node.js installed on your computer. Once that's ready, open your terminal and run this command in a new folder:

npm init playwright@latest

This command kicks off an installer that will ask you a few questions, such as whether to use TypeScript or JavaScript and where to place your tests. It creates a basic project structure for you, including configuration files and even an example test.

Lesson image

Let's write a simple test to see it in action. Create a file named example.spec.js inside the tests folder that the installer created.

// tests/example.spec.js

import { test, expect } from '@playwright/test';

test('should have the correct title', async ({ page }) => {
  // 1. Navigate to the Playwright website
  await page.goto('https://playwright.dev/');

  // 2. Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);
});

This script does two things:

  1. It navigates to the official Playwright website.
  2. It checks that the page title contains the word "Playwright". This is called an assertion—it confirms that an expected condition is true.

To run this test, go back to your terminal and use this command:

npx playwright test

Playwright will launch a browser, run the steps in your test file, and report the results in your terminal. If all goes well, you'll see a message indicating that your test passed. You've just written and run your first automated web test!

Quiz Questions 1/6

What is the primary purpose of the Playwright framework?

Quiz Questions 2/6

What is the key feature in Playwright that helps prevent 'flaky' tests by waiting for elements to be interactive before performing actions?