Warning: Many parts of this tutorial have fallen out of date. I hope to update it sometime in the future.
Everybody gets frustrated when web apps are broken, but testing them thoroughly doesn't need to be a chore. Playwright, a new open-source browser automation tool from Microsoft, makes testing web apps fun! Playwright outperforms other browser automation with a slew of nifty features like automatic waiting, mobile emulation, and network interception. Plus, with isolated browser contexts, Playwright tests can set up much faster than traditional Web UI tests.
In this tutorial, we will build a Python test automation project from the ground up using Playwright. We will automate web tests together step-by-step using Playwright for interactions and pytest for execution. We'll also explore Playwright tricks like cross-browser testing, capturing videos, and even running tests in parallel! By the end of this tutorial, you'll be empowered to test modern web apps with modern web test tools. You'll also have an example project to be the foundation for your future tests.
Playwright is a fairly new test automation framework from Microsoft. It is open source, and it has bindings in TypeScript/JavaScript, Python, .NET, and Java. Some of the nice features Playwright offers include:
- concise, readable calls
- easy out-of-the-box setup
- very fast execution times (compared to other browser automation tools)
- cross-browser and mobile emulation support
- automatic waiting
- screenshots and video capture
- built-in API calls
Microsoft is actively developing Playwright, so new features are coming all the time!
You can take this tutorial independently by following the instructions
in this README
and in the tutorial
folder.
Feel free to open issues against this repository if you have any trouble completing the tutorial independently.
I also have given (or will be giving) this tutorial as a live workshop at the following events:
- TAU: The Homecoming (December 1, 2021)
- Python Web Conference 2022 (March 22, 2022)
- STAREast 2022 (April 26, 2022)
- PyCon US 2022 (April 28, 2022)
This tutorial has six main parts, each with three sections:
- Getting started
- What is Playwright?
- Our web search test
- Test project setup
- First steps with Playwright
- Browsers, contexts, and pages
- Navigating to a web page
- Performing a search
- Writing assertions
- Checking the search field
- Checking the result links
- Checking the title
- Refactoring using page objects
- The search page
- The result page
- Page object fixtures
- Nifty Playwright tricks
- Testing different browsers
- Capturing screenshots and videos
- Running tests in parallel
- Testing with APIs
- API setup
- Writing a pure API test
- Writing a hybrid UI/API test
You must have basic Python programming skills to complete this tutorial. If you are new to Python, check out the free Python Programming course on Test Automation University.
Your machine must also have Python 3.7 or higher installed. You can download the latest version of Python from Python.org.
You should also have a decent Python editor like Visual Studio Code or PyCharm.
If you wish to complete Part 6 ("Testing with APIs"), then you will need a GitHub account.
Each tutorial part has a corresponding branch in this repository containing the part's example code and tutorial
instructions.
The branches allow you to check your progress at any point during the tutorial.
The branch names are:
Part | Branch |
---|---|
Start | 0-initial-project |
Part 1 | 1-getting-started |
Part 2 | 2-first-steps |
Part 3 | 3-assertions |
Part 4 | 4-page-objects |
Part 5 | 5-playwright-tricks |
Part 6 | 6-api-testing |
Complete | main |