-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplaywright-sample.py
More file actions
executable file
·98 lines (71 loc) · 2.75 KB
/
playwright-sample.py
File metadata and controls
executable file
·98 lines (71 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "playwright",
# ]
# ///
# See https://docs.astral.sh/uv/guides/scripts/#using-a-shebang-to-create-an-executable-file
"""playwright-sample.py using uv to setup browser test automation using Playwright open-sourced by Microsoft.
BEFORE RUNNING, on Terminal:
# cd to a folder to receive
git clone https://github.com/wilsonmar/python-samples.git --depth 1
cd python-samples
python3 -m pip install uv
python -m venv .venv # creates bin, include, lib, pyvenv.cfg
uv venv .venv
source .venv/bin/activate
uv add playwright --frozen
python3 -m playwright install # to install on browsers
ruff check playwright-sample.py
chmod +x playwright-sample.py
uv run playwright-sample.py
# Terminal does not freeze.
# Press control+C to cancel/interrupt run.
AFTER RUN:
deactivate # uv
rm -rf .venv .pytest_cache __pycache__
"""
__last_change__ = "26-03-29 v001 new :playwright-sample.py"
__status__ = "WORKS on macOS Sequoia 15.6.1"
from playwright.sync_api import sync_playwright, Playwright
def run_sample(playwright: Playwright) -> str:
"""Run Playwright."""
# with sync_playwright() as p:
if use_browser == "firefox":
browser = playwright.firefox.launch()
elif use_browser == "chromium":
browser = playwright.chromium.launch()
elif use_browser == "webkit":
browser = playwright.webkit.launch()
page = browser.new_page()
page.goto(use_url)
title = page.title()
print(f"run_sample({use_browser} at {use_url}) page.title: \"{title}\"")
assert title == page_title_expected, f"FAIL {use_browser}: expected \"{page_title_expected}\", got \"{title}\""
print(f"PASS {use_browser}: page.title == \"{page_title_expected}\"")
# TODO: other actions...
browser.close()
return True
with sync_playwright() as playwright:
use_url="https://google.com"
# google.com contains text "Google".
page_title_expected="Google"
use_browser="firefox"
response=run_sample(playwright)
use_browser="chromium"
response=run_sample(playwright)
use_browser="webkit"
response=run_sample(playwright)
"""
# TODO: Additional targets
# use_url="https://example.com"
page_title_expected="Example Domain"
# TODO: See https://wilsonmar.github.io/flood-the-internet/#playwright
https://medium.com/@modirahul2019/building-a-robust-automation-framework-with-playwright-and-python-99bc27989325
https://www.thoughtworks.com/radar/languages-and-frameworks/playwright
https://www.scrapingbee.com/blog/playwright-for-python-web-scraping/
https://github.com/nirtal85/Playwright-Python-Example
https://playwright.dev/python/
https://playwright.dev/python/docs/api/class-playwright
"""