Skip to content

Commit ae9094a

Browse files
committed
Add codegen til
1 parent b618357 commit ae9094a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Use Playwright Codegen to Automatically Record UI Interactions
2+
3+
Playwright has a neat feature of recording user interaction and automatically
4+
generate source code for you. This would save you a lot of time and automate many
5+
of your day to day work if the tasks can be done by a browser.
6+
7+
The following example will show you how to record an UI operation that we visit
8+
the playwright.dev docs and search for this codegen feature and then move on to
9+
the next page:
10+
11+
First, create a venv:
12+
13+
```sh
14+
python3 -m venv venv
15+
```
16+
17+
Then, install the package
18+
19+
```sh
20+
pip install pytest-playwright
21+
```
22+
23+
And run the codegen command:
24+
25+
```sh
26+
playwright codegen
27+
```
28+
29+
> Note that you might need to run `playwright install` command if you haven't installed any chromium browser on your machine before
30+
31+
After running the codegen, you should now have two browsers opened, one is the main chromium browser and the other is a UI for recorded actions.
32+
33+
Here is a piece of code that's generated by the codegen after running thru the actions that we mentioned in the above:
34+
35+
```py
36+
import time
37+
38+
from playwright.sync_api import Playwright, expect, sync_playwright
39+
40+
41+
def run(playwright: Playwright) -> None:
42+
browser = playwright.chromium.launch(headless=False)
43+
context = browser.new_context()
44+
page = context.new_page()
45+
page.goto("https://playwright.dev/")
46+
page.get_by_role("button", name="Search").click()
47+
page.get_by_placeholder("Search docs").fill("codegen")
48+
page.locator("#docsearch-item-0").get_by_role("link",
49+
name="Running Codegen​ Test generator").click()
50+
page.get_by_role(
51+
"heading", name="Running CodegenDirect link to Running Codegen").click()
52+
53+
page.mouse.wheel(0, 4000)
54+
55+
# These sleeps help to slow down the UI interactions so you can see what's actually done during the run
56+
time.sleep(2)
57+
page.locator(".pagination-nav__link--next").click()
58+
time.sleep(3)
59+
60+
# ---------------------
61+
context.close()
62+
browser.close()
63+
64+
65+
with sync_playwright() as playwright:
66+
run(playwright)
67+
68+
```
69+
70+
References:
71+
72+
- [THIS is Playwrights BEST Feature for Web Automation
73+
](https://www.youtube.com/watch?v=oPjfDkge8Vc&t=14s&ab_channel=JohnWatsonRooney) - Awesome video tutorial by John Rooney on how to work with codegen
74+
- [Running Codegen](https://playwright.dev/docs/codegen-intro#running-codegen) - Playwright docs for how to run codegen

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Inspired by [Josh Branchaud](https://dev.to/jbranchaud/how-i-built-a-learning-ma
1616
- [Display Web Scraping Data with Pandas](/python/display-web-scraping-data-with-pandas.md)
1717
- [Scrape Data From an API Endpoint](/python/scrape-data-from-an-api-endpoint.md)
1818
- [Scrape Dynamic Data with Playwright](/python/scrape-dynamic-data-with-playwright.md)
19+
- [Use Playwright Codegen to Automatically Record UI Interactions](python/user-playwright-codegen-to-automatically-record-ui-interactions.md)
1920

2021
## CSS
2122

0 commit comments

Comments
 (0)