Skip to content

Commit df2141e

Browse files
Wrote README Part 1
1 parent 5a6cc94 commit df2141e

File tree

1 file changed

+154
-3
lines changed

1 file changed

+154
-3
lines changed

README.md

Lines changed: 154 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ You should also have a decent Python editor like
4040
[Visual Studio Code](https://code.visualstudio.com/docs/languages/python)
4141
or [PyCharm](https://www.jetbrains.com/pycharm/).
4242

43+
The command line shown in examples below is [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)).
44+
If you are using a different shell or a Windows command line, some commands may need to be different.
45+
4346

4447
### Agenda
4548

@@ -48,8 +51,7 @@ This workshop has five main parts:
4851
1. Getting started
4952
1. What is Playwright?
5053
2. Our web search test
51-
3. Installations
52-
4. Test project setup
54+
3. Test project setup
5355
2. First steps with Playwright
5456
1. Raw Playwright calls
5557
3. Refactoring using page objects
@@ -88,7 +90,156 @@ You can code along with them either during the live workshop event or afterwards
8890

8991
### Part 1: Getting started
9092

91-
TBD
93+
94+
#### What is Playwright?
95+
96+
[Playwright](https://playwright.dev/python/) is a new library that can automate interactions with Chromium, Firefox, and WebKit browsers via a single API.
97+
It is an open source project developed by Microsoft.
98+
99+
Playwright is a fantastic alternative to [Selenium WebDriver](https://www.selenium.dev/) for web UI testing.
100+
Like Selenium WebDriver, Playwright has language bindings in multiple languages: Python, .NET, Java, and JavaScript.
101+
Playwright also refines many of the pain points in Selenium WebDriver.
102+
Some examples include:
103+
104+
* Playwright interactions automatically wait for elements to be ready.
105+
* Playwright can use one browser instance with multiple browser contexts for isolation instead of requiring multiple instances.
106+
* Playwright has device emulation for testing responsive web apps in mobile browsers.
107+
108+
For a more thorough list of advantages, check out
109+
[Why Playwright?](https://playwright.dev/python/docs/why-playwright/)
110+
from the docs.
111+
112+
113+
#### Our web search test
114+
115+
For this workshop, we will walk through one test scenario for DuckDuckGo searching.
116+
[DuckDuckGo](https://duckduckgo.com/) is a search engine like Google or Yahoo.
117+
118+
The steps for a basic DuckDuckGo search are:
119+
120+
```gherkin
121+
Given the DuckDuckGo home page is displayed
122+
When the user searches for a phrase
123+
Then the search result query is the phrase
124+
And the search result links pertain to the phrase
125+
And the search result title contains the phrase
126+
```
127+
128+
Go to [duckduckgo.com](https://duckduckgo.com/) and give this a try.
129+
You can use any search phrase you like.
130+
It is important to write a test *case* before writing test *code*.
131+
It is also important to try a test manually before attempting to automate it.
132+
133+
134+
#### Test project setup
135+
136+
Let's set up the test project!
137+
For this workshop, we will build a new project from the ground up.
138+
The GitHub repository should be used exclusively as a reference for example code.
139+
140+
Create a directory named `tau-playwright-workshop` for the project:
141+
142+
```bash
143+
$ mkdir tau-playwright-workshop
144+
$ cd tau-playwright-workshop
145+
```
146+
147+
Inside this project, create a [Python virtual environment](https://docs.python.org/3/tutorial/venv.html)
148+
using the [venv](https://docs.python.org/3/library/venv.html) module
149+
to manage dependency packages locally:
150+
151+
```bash
152+
$ python3 -m venv venv
153+
$ source venv/bin/activate
154+
```
155+
156+
Creating a new virtual environment for each Python project is a recommended practice.
157+
This command will create a subdirectory named `venv` that holds all virtual environment files, including dependency packages.
158+
After creating a virtual environment, you must "activate" it to use it using the `source` command shown above.
159+
You can tell if a virtual environment is active if its name appears in the bash prompt.
160+
161+
*A note about Python commands:*
162+
Python has two incompatible major versions: 2 and 3.
163+
Although Python 2 end-of-life was January 1, 2020, many machines still run it.
164+
For example, macOS comes bundled with Python 2.7.18.
165+
Sometimes, the `python` executable may point to Python 2 instead of 3.
166+
To be precise about versions and executables, we will use the `python3` and `pip3` commands explicitly in this workshop.
167+
168+
Let's add some Python packages to our new virtual environment:
169+
170+
```bash
171+
$ pip3 install playwright
172+
$ pip3 install pytest
173+
$ pip3 install pytest-playwright
174+
```
175+
176+
By itself, Playwright is simply a library for browser automation.
177+
We need a test framework like pytest if we want to automate tests.
178+
The [`pytest-playwright`](https://playwright.dev/python/docs/test-runners)
179+
is a pytest plugin developed by the Playwright team that simplifies Playwright integration.
180+
181+
You can check all installed packages using `pip3 freeze`.
182+
They should look something like this:
183+
184+
```bash
185+
$ pip3 freeze
186+
attrs==21.2.0
187+
greenlet==1.1.2
188+
iniconfig==1.1.1
189+
packaging==21.3
190+
playwright==1.17.0
191+
pluggy==1.0.0
192+
py==1.11.0
193+
pyee==8.2.2
194+
pyparsing==3.0.6
195+
pytest==6.2.5
196+
toml==0.10.2
197+
websockets==10.1
198+
```
199+
200+
Notice that pip fetches dependencies of dependencies.
201+
202+
After the Python packages are installed, we need to install the browsers for Playwright.
203+
The `playwright install` command installs the latest versions of the three browsers that Playwright supports:
204+
Chromium, Firefox, and WebKit:
205+
206+
```bash
207+
$ playwright install
208+
```
209+
210+
By default, pytest with the Playwright plugin will run headless Chromium.
211+
We will show how to run against other browsers in Part 5.
212+
213+
Finally, let's create a test function stub.
214+
By Python conventions, all tests should be located under a `tests` directory.
215+
Create a `tests` directory, and inside, create a file named `test_search.py`:
216+
217+
```bash
218+
$ mkdir tests
219+
$ touch tests/test_search.py
220+
```
221+
222+
Add the following code to `tests/test_search.py`:
223+
224+
```python
225+
def test_basic_duckduckgo_search():
226+
# Given the DuckDuckGo home page is displayed
227+
# When the user searches for a phrase
228+
# Then the search result query is the phrase
229+
# And the search result links pertain to the phrase
230+
# And the search result title contains the phrase
231+
pass
232+
```
233+
234+
The `test_basic_duckduckgo_search` is merely a stub, but it establishes good practices:
235+
236+
* It has a clear name.
237+
* It defines the behavior to test step-by-step in its comments.
238+
* It can be run immediately.
239+
240+
The `pass` statement at the end is just a no-op.
241+
242+
Remember, write test *cases* before you write test *code*.
92243

93244

94245
### Part 2: First steps with Playwright

0 commit comments

Comments
 (0)