Skip to content

Playwright QuickStart

Published: at 03:22 PMSuggest Changes

Playwright is a premier tool for browser automation. While there are competitors like Cypress and WebdriverIO, choosing Playwright ensures you won’t be disappointed. It has everything you need for end-to-end testing of web applications and maybe even more.

Let’s dive into this quick guide and get you up to speed.

Table of contents

Open Table of contents

Install Node.js

First of all, you need to have Node.js installed on your system. The best way to handle this is by using nvm.

nvm (Node Version Manager) - is a CLI tool that allows you to install and manage multiple versions of Node.js. Install the current LTS Node.js by using the --lts flag. Or you can install a specific version by adding the version number after nvm install.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc
nvm install --lts

Verify the installation by listing the installed versions and checking the Node.js version output.

nvm ls
node --version

Install package manager

In order to handle your project dependencies, you need to have a package manager installed. You can choose between npm, yarn or pnpm. I will use npm in the scope of this article. You don’t need to install it separately, it comes automatically as part of Node.js.

Install Playwright

Playwright has init command that will create a basic folder structure for you.

mkdir playwright
cd playwright
npm init playwright@latest -y

When you run it, it will guide you through a simple wizard to set up the project.

You should see files and folders created in your project directory after answering the questions.

playwright
├─ .github
│  └─ workflows
│     └─ playwright.yml
├─ node_modules
├─ tests
│  └─ example.spec.ts
├─ tests-examples
|  └─ demo-todo-app.spec.ts
├─ .gitignore
├─ package-lock.json
├─ package.json
└─ playwright.config.ts

Finally, install system dependencies using sudo

sudo npx playwright install-deps

Run tests from terminal

By default, Playwright executes tests across three browsers: Chromium, Firefox, and WebKit, utilizing three workers concurrently. This behavior can be customized in the playwright.config file.

Tests are executed in headless mode, meaning the browsers will not open during test execution. Test results and logs are displayed in the terminal. --headed flag will open the browser during test execution.

npx playwright test --headed

Install VSCode extension

Playwright has an official extension for Visual Studio Code that provides code snippets, IntelliSense, and debugging support. Install the extension from the VSCode Extensions tab. After installation, you will be able to run tests directly from the editor.

Playwright VSCode extension

Key features

Test Design Best Practices

It is essential to follow best practices when setting up an end-to-end framework for your project. Whether you are a tester working on a dedicated QA team, adding tests in collaboration with developers, or working as a solo entrepreneur, you need to follow these guidelines. Otherwise, you will end up with flaky or brittle tests and spend more time maintaining them than receiving effective feedback.

Conclusion

Playwright provides powerful tools for modern web application testing. With this guide, you’ve learned to set up a project, run tests, and implement best practices.

Example project with tests is available here.


Previous Post
Java Test Automation Framework