Skip to content

Commit 720495c

Browse files
Merge branch '4-page-objects' into 5-playwright-tricks
2 parents 4158c3a + 14560b6 commit 720495c

16 files changed

+106
-64
lines changed

README.md

+62-38
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,66 @@
1-
![Workshop card](workshop/images/workshop-card.png)
1+
![Playwright banner](tutorial/images/playwright-banner.jpeg)
22

3-
# tau-playwright-workshop
43

5-
This repository contains the instructions and example code for the Playwright workshop
6-
for [TAU: The Homecoming](https://applitools.com/tau-homecoming/)
7-
on December 1, 2021.
8-
The workshop will be done in [Python](https://www.python.org/).
4+
# Tutorial: Web Testing with Playwright in Python
95

106

11-
## Instructions
7+
## Abstract
128

13-
Please try to attend the workshop *live* on the day of the event.
14-
However, if you cannot make it, never fear!
15-
**You can still take the workshop as a self-guided tutorial.**
16-
Start by reading this README.
17-
Then, follow the guides in the `workshop` folder.
18-
Each part of the workshop has a `workshop` guide with full instructions.
19-
Feel free to open issues against this repository if you have any trouble completing the workshop independently.
9+
Everybody gets frustrated when web apps are broken,
10+
but testing them thoroughly doesn't need to be a chore.
11+
[Playwright](https://playwright.dev/python/),
12+
a new open-source browser automation tool from Microsoft,
13+
makes testing web apps fun!
14+
Playwright outperforms other browser automation with a slew of nifty features
15+
like automatic waiting, mobile emulation, and network interception.
16+
Plus, with isolated browser contexts,
17+
Playwright tests can set up *much* faster than traditional Web UI tests.
2018

19+
In this tutorial, we will build a [Python](https://www.python.org/)
20+
test automation project from the ground up using Playwright.
21+
We will automate web search engine tests together step-by-step
22+
using Playwright for interactions and pytest for execution.
23+
We'll also explore Playwright tricks
24+
like cross-browser testing, capturing videos, and even running tests in parallel!
25+
By the end of this tutorial, you'll be empowered to test modern web apps with modern web test tools.
26+
You'll also have an example project to be the foundation for your future tests.
2127

22-
## Abstract
2328

24-
[Playwright](https://playwright.dev/python/)
25-
is a hot new browser automation tool from Microsoft.
26-
With bindings for .NET, Java, JavaScript, and Python, it’s a strong alternative to Selenium WebDriver for end-to-end web app testing.
29+
## What is Playwright?
2730

28-
This workshop will be an introduction to Playwright using [Python](https://www.python.org/).
29-
We will automate a test scenario together that performs a [DuckDuckGo](https://duckduckgo.com/) search.
30-
As we code along the test together, we will learn:
31+
Playwright is a fairly new test automation framework from Microsoft.
32+
It is open source, and it has bindings in TypeScript/JavaScript, Python, .NET, and Java.
33+
Some of the nice features Playwright offers include:
3134

32-
* How to install and configure Playwright
33-
* How to integrate Playwright with [pytest](https://docs.pytest.org/), Python’s leading test framework
34-
* How to perform interactions through page objects
35-
* How to conveniently run different browsers, capture videos, and run tests in parallel
35+
* concise, readable calls
36+
* easy out-of-the-box setup
37+
* very fast execution times (compared to other browser automation tools)
38+
* cross-browser and mobile emulation support
39+
* automatic waiting
40+
* built-in API calls
41+
* screenshots and video capture
3642

37-
Come prepared with Python 3.7 or higher installed on your machine.
38-
By the end of the workshop, you will have a solid foundation in Playwright as well as a Python project you can extend with new tests!
43+
Microsoft is actively developing Playwright,
44+
so new features are coming all the time!
3945

4046

41-
## Prerequisites
47+
## Tutorial Instructions
4248

43-
To code along with this workshop, your machine must have Python 3.7 or higher.
44-
You should also have a decent Python editor like
45-
[Visual Studio Code](https://code.visualstudio.com/docs/languages/python)
46-
or [PyCharm](https://www.jetbrains.com/pycharm/).
49+
You can take this tutorial independently by following the instructions
50+
in this `README` and in the [`tutorial`](tutorial) folder.
51+
Feel free to open issues against this repository if you have any trouble completing the tutorial independently.
4752

48-
The command line shown in examples below is [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)).
49-
If you are using a different shell or a Windows command line, some commands may need to be different.
53+
I also have given (or will be giving) this tutorial as a live workshop at the following events:
5054

55+
* [TAU: The Homecoming](https://applitools.com/on-demand-videos/tau-the-homecoming-2021/) (December 1, 2021)
56+
* [Python Web Conference 2022](https://2022.pythonwebconf.com/tutorials/end-to-end-testing-with-playwright) (March 22, 2021)
57+
* [STAREast 2022](https://stareast.techwell.com/program/tutorials/web-ui-testing-playwright-python-stareast-2022) (April 26, 2022)
58+
* [PyCon US 2022](https://us.pycon.org/2022/schedule/presentation/35/) (April 28, 2022)
5159

52-
## Agenda
5360

54-
This workshop has five main parts, each with three sections:
61+
## Outline
62+
63+
This tutorial has five main parts, each with three sections:
5564

5665
1. Getting started
5766
1. What is Playwright?
@@ -75,10 +84,25 @@ This workshop has five main parts, each with three sections:
7584
3. Running tests in parallel
7685

7786

87+
## Prerequisites
88+
89+
You must have basic Python programming skills to complete this tutorial.
90+
If you are new to Python, check out the free
91+
[Python Programming](https://testautomationu.applitools.com/python-tutorial/) course
92+
on Test Automation University.
93+
94+
Your machine must also have Python 3.7 or higher installed.
95+
You can download the latest version of Python from [Python.org](https://www.python.org/).
96+
97+
You should also have a decent Python editor like
98+
[Visual Studio Code](https://code.visualstudio.com/docs/languages/python)
99+
or [PyCharm](https://www.jetbrains.com/pycharm/).
100+
101+
78102
## Example code branches
79103

80-
Each workshop part has a corresponding branch in this repository containing the part's example code and `workshop` instructions.
81-
The branches allow you to check your progress at any point during the workshop.
104+
Each tutorial part has a corresponding branch in this repository containing the part's example code and `tutorial` instructions.
105+
The branches allow you to check your progress at any point during the tutorial.
82106
The branch names are:
83107

84108
| Part | Branch |

requirements.txt

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ greenlet==1.1.2
66
idna==3.3
77
iniconfig==1.1.1
88
packaging==21.3
9-
playwright==1.17.0
9+
playwright==1.19.1
1010
pluggy==1.0.0
1111
py==1.11.0
12-
pyee==8.2.2
12+
pyee==8.1.0
1313
pyparsing==3.0.6
14-
pytest==6.2.5
14+
pytest==7.0.1
1515
pytest-base-url==1.4.2
16-
pytest-forked==1.3.0
17-
pytest-playwright==0.2.2
18-
pytest-xdist==2.4.0
16+
pytest-forked==1.4.0
17+
pytest-playwright==0.2.3
18+
pytest-xdist==2.5.0
1919
python-slugify==5.0.2
2020
requests==2.26.0
2121
text-unidecode==1.3
2222
toml==0.10.2
23+
tomli==2.0.1
2324
urllib3==1.26.7
2425
websockets==10.1

workshop/1-getting-started.md renamed to tutorial/1-getting-started.md

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Part 1: Getting started
22

3-
Part 1 of the workshop explains how to set up a Python test automation project with pytest and Playwright.
3+
Part 1 of the tutorial explains how to set up a Python test automation project with pytest and Playwright.
44

55

66
## What is Playwright?
@@ -24,7 +24,7 @@ from the docs.
2424

2525
## Our web search test
2626

27-
For this workshop, we will walk through one test scenario for DuckDuckGo searching.
27+
For this tutorial, we will walk through one test scenario for DuckDuckGo searching.
2828
[DuckDuckGo](https://duckduckgo.com/) is a search engine like Google or Yahoo.
2929

3030
The steps for a basic DuckDuckGo search are:
@@ -46,14 +46,14 @@ It is also important to try a test manually before attempting to automate it.
4646
## Test project setup
4747

4848
Let's set up the test project!
49-
For this workshop, we will build a new project from the ground up.
49+
For this tutorial, we will build a new project from the ground up.
5050
The GitHub repository should be used exclusively as a reference for example code.
5151

52-
Create a directory named `tau-playwright-workshop` for the project:
52+
Create a directory named `playwright-python-tutorial` for the project:
5353

5454
```bash
55-
$ mkdir tau-playwright-workshop
56-
$ cd tau-playwright-workshop
55+
$ mkdir playwright-python-tutorial
56+
$ cd playwright-python-tutorial
5757
```
5858

5959
Inside this project, create a [Python virtual environment](https://docs.python.org/3/tutorial/venv.html)
@@ -72,7 +72,7 @@ Python has two incompatible major versions: 2 and 3.
7272
Although Python 2 end-of-life was January 1, 2020, many machines still run it.
7373
For example, macOS comes bundled with Python 2.7.18.
7474
Sometimes, the `python` executable may point to Python 2 instead of 3.
75-
To be precise about versions and executables, we will use the `python3` and `pip3` commands explicitly in this workshop.
75+
To be precise about versions and executables, we will use the `python3` and `pip3` commands explicitly in this tutorial.
7676

7777
After creating a virtual environment, you must "activate" it.
7878
On macOS or Linux, use the following command:
@@ -97,6 +97,11 @@ $ pip3 install pytest
9797
$ pip3 install pytest-playwright
9898
```
9999

100+
> If you want to run the tests from this repository instead of creating a fresh project,
101+
> install the dependencies using this command:
102+
>
103+
> `$ pip3 install -r requirements.txt`
104+
100105
By itself, Playwright is simply a library for browser automation.
101106
We need a test framework like pytest if we want to automate tests.
102107
The [`pytest-playwright`](https://playwright.dev/python/docs/test-runners)
@@ -114,18 +119,19 @@ greenlet==1.1.2
114119
idna==3.3
115120
iniconfig==1.1.1
116121
packaging==21.3
117-
playwright==1.17.0
122+
playwright==1.19.1
118123
pluggy==1.0.0
119124
py==1.11.0
120-
pyee==8.2.2
125+
pyee==8.1.0
121126
pyparsing==3.0.6
122-
pytest==6.2.5
127+
pytest==7.0.1
123128
pytest-base-url==1.4.2
124-
pytest-playwright==0.2.2
129+
pytest-playwright==0.2.3
125130
python-slugify==5.0.2
126131
requests==2.26.0
127132
text-unidecode==1.3
128133
toml==0.10.2
134+
tomli==2.0.1
129135
urllib3==1.26.7
130136
websockets==10.1
131137
```

workshop/2-first-steps.md renamed to tutorial/2-first-steps.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Part 2: First steps with Playwright
22

3-
Part 2 of the workshop shows how to take your first steps with Playwright calls.
3+
Part 2 of the tutorial shows how to take your first steps with Playwright calls.
44
It will explain browsers, contexts, and pages.
55
It will also cover basic Playwright API calls.
66

@@ -136,14 +136,15 @@ Out of the box, Playwright supports the following types of selectors:
136136
* N-th element
137137
* React
138138
* Vue
139+
* ID attributes
139140

140141
Text and CSS selectors also pierce the Shadow DOM by default!
141142

142143
In general, you should keep selectors as simple as possible.
143144
Try to stick to text, IDs, or CSS selectors.
144145
Use more complicated selectors only as necessary.
145146

146-
This workshop will not cover recommended practices for element selectors deeply.
147+
This tutorial will not cover recommended practices for element selectors deeply.
147148
If you want to learn more about selectors,
148149
read the [Element selectors](https://playwright.dev/python/docs/selectors) page in the Playwright docs,
149150
or take the [Web Element Locator Strategies](https://testautomationu.applitools.com/web-element-locator-strategies/) course
@@ -175,6 +176,11 @@ Append the following line to the test case:
175176
page.fill('#search_form_input_homepage', 'panda')
176177
```
177178

179+
> Since `search_form_input_homepage` is an ID, we could also use Playwright's
180+
> [ID attribute selector](https://playwright.dev/python/docs/selectors#id-data-testid-data-test-id-data-test-selectors):
181+
>
182+
> `page.fill('id=search_form_input_homepage', 'panda')`
183+
178184
Using Selenium WebDriver, we would need to locate the element and then send the interaction to it.
179185
However, in Playwright, these two parts are combined into a single call.
180186
Furthermore, Playwright waits for the target element to be visible and editable before it attempts to enter the text.
@@ -222,4 +228,4 @@ Now, you should see the test actually perform the search!
222228
Navigation, input filling, and clicking are only three of many page interactions you can do with Playwright.
223229
Anything a user can do on a web page, Playwright can do as well.
224230
Check out the Playwright [Page](https://playwright.dev/python/docs/api/class-page) API to see *all* methods and attributes.
225-
We will use more of these calls in the next workshop parts.
231+
We will use more of these calls in the next tutorial parts.

workshop/3-assertions.md renamed to tutorial/3-assertions.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Part 3: Writing assertions
22

3-
Part 3 of the workshop focuses on making assertions about the result page after performing a search.
3+
Part 3 of the tutorial focuses on making assertions about the result page after performing a search.
44
Waiting becomes a much bigger concern for these steps, but Playwright makes it easy.
55

66

@@ -245,8 +245,13 @@ Rerun the test again to make sure it works.
245245
If it does, congrats!
246246
You have just completed a full test case in Python using Playwright with pytest.
247247

248+
Playwright's [`Page`](https://playwright.dev/python/docs/api/class-page) class
249+
provides several methods for interacting with pages and getting state from them.
250+
Read the docs to familiarize yourself with them.
251+
`Page` provides methods to interact with *every* type of web element.
252+
248253
Notice how concise this code is.
249254
Unfortunately, it's not very reusable.
250255
If other tests needed to perform DuckDuckGo searches,
251256
they would duplicate similar calls.
252-
In the next workshop part, we will refactor this test using page objects to make the code more reusable and extendable.
257+
In the next tutorial part, we will refactor this test using page objects to make the code more reusable and extendable.

workshop/4-page-objects.md renamed to tutorial/4-page-objects.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Create a new directory named `pages`, and inside it, create blank files with the
2929
Your project's directory layout should look like this:
3030

3131
```
32-
tau-playwright-workshop
32+
playwright-python-tutorial
3333
├── pages
3434
│ ├── __init__.py
3535
│ ├── search.py
@@ -77,7 +77,7 @@ Let's also add the DuckDuckGo URL:
7777

7878
*(Warning:
7979
Base URLs should typically be passed into automation code as an input, not hard-coded in a page object.
80-
We are doing this here as a matter of simplicity for this workshop.)*
80+
We are doing this here as a matter of simplicity for this tutorial.)*
8181

8282
Next, let's handle dependency injection for the browser automator.
8383
Since each test will have its own Playwright page, we should inject that page.
@@ -90,7 +90,7 @@ Add the following initializer method to the class:
9090
```
9191

9292
The `__init__` method is essentially a constructor for Python classes
93-
(but with a bit of nuance that doesn't matter for this workshop).
93+
(but with a bit of nuance that doesn't matter for this tutorial).
9494
It has one argument named `page` for the Playwright page,
9595
which it stores as an instance variable (via `self`).
9696

@@ -335,7 +335,7 @@ Create a new file at `tests/conftest.py`.
335335
The new project directory layout should look like this:
336336

337337
```
338-
tau-playwright-workshop
338+
playwright-python-tutorial
339339
├── pages
340340
│ ├── __init__.py
341341
│ ├── search.py
File renamed without changes.
76.5 KB
Loading

workshop/images/workshop-card.png

-292 KB
Binary file not shown.

0 commit comments

Comments
 (0)