Playwright Introduction
What is Playwright?
Playwright is a tool for testing websites in real browsers.
It is made by Microsoft, and it is free and open source.
Playwright can control the three main browser engines:
- Chromium, used by Google Chrome and Microsoft Edge
- Firefox
- WebKit, used by Safari
You write a script that tells the browser what to do, and Playwright does it, much faster than a person could.
What is End-to-End Testing?
An end-to-end test checks a whole feature the way a user uses it.
It opens the page, clicks buttons, fills in forms, and checks what the page shows.
For example, an end-to-end test for a login page could:
- Open the login page
- Type a username and a password
- Click the "Log in" button
- Check that the page says "Welcome, student!"
A unit test checks one small function on its own.
An end-to-end test checks that all the parts work together: the HTML, the CSS, the JavaScript and the server.
What Can You Do with Playwright?
- Test websites: check that pages and features work after every change
- Take screenshots and videos of pages and test runs
- Compare screenshots to find visual changes
- Test APIs: send HTTP requests and check the responses
- Automate tasks: fill in forms, download files, or collect data from web pages (web scraping)
This tutorial is about testing.
It uses Playwright Test, the test runner that comes with Playwright.
Playwright, Selenium and Cypress
Selenium and Cypress are two other popular tools for end-to-end testing.
All three can test a website in a real browser.
| Playwright | Selenium | Cypress | |
|---|---|---|---|
| Languages | JavaScript, TypeScript, Python, Java, C# | Java, Python, C#, JavaScript, Ruby | JavaScript, TypeScript |
| Browsers | Chromium, Firefox, WebKit | Chrome, Edge, Firefox, Safari | Chrome, Edge, Firefox (WebKit is experimental) |
| Waits for elements | Yes, built in | You add waits yourself | Yes, built in |
| Test runner included | Yes | No, you use one from your language | Yes |
Selenium is the oldest, and supports the most languages.
Cypress runs inside the browser, and is popular with front-end developers.
Playwright runs outside the browser, can use several tabs and browsers in one test, and comes with its own tools.
Languages
Playwright works with several programming languages:
- JavaScript and TypeScript, with the Playwright Test runner
- Python
- Java
- .NET (C#)
JavaScript and TypeScript get new features first, and have the most tools.
This tutorial uses JavaScript.
The last chapters show how to use Playwright with Python.
How a Test Runs
When you run your tests, three things work together:
- The test runner finds your test files, runs the tests and reports the results
- The browser is started by Playwright, usually without a visible window
- The page is a browser tab that your test controls
Each test gets a new, clean page.
The test tells the page what to do, and then checks the result:
Example
test('can log in', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/login.html');
await page.getByLabel('Username').fill('student');
await page.getByLabel('Password').fill('w3schools');
await page.getByRole('button', { name: 'Log in' }).click();
await expect(page.getByTestId('welcome')).toHaveText('Welcome, student!');
});
This test opens the login page, types a username and a password, clicks "Log in", and checks the welcome message.
You do not need to understand every line yet.
You will set up a project and write your first test in the next chapters.