Skip to content

Commit 11db918

Browse files
authored
Merge pull request #2 from ks6088ts-labs/feature/issue-1_setup-playwright
set up playwright
2 parents fd99aaa + 43d5c0e commit 11db918

File tree

7 files changed

+286
-29
lines changed

7 files changed

+286
-29
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,5 @@ cython_debug/
162162
# Project
163163
*.env
164164
requirements.txt
165+
generated
166+
tests/test_codegen.py

Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ info: ## show information
1616
install-deps-dev: ## install dependencies for development
1717
uv sync --all-extras
1818
uv run pre-commit install
19+
uv run playwright install
1920

2021
.PHONY: install-deps
2122
install-deps: ## install dependencies for production
@@ -98,3 +99,28 @@ docs-serve: ## serve documentation
9899

99100
.PHONY: ci-test-docs
100101
ci-test-docs: docs ## run CI test for documentation
102+
103+
# ---
104+
# Project
105+
# ---
106+
107+
.PHONY: test-verbose
108+
test-verbose: ## run tests with verbose
109+
uv run pytest \
110+
--capture=no \
111+
--verbose \
112+
--headed \
113+
--tracing on \
114+
--video on \
115+
--screenshot on \
116+
--output generated
117+
118+
TRACE_ZIP ?= generated/tests-test-core-py-test-get-started-link-chromium/trace.zip
119+
.PHONY: show-trace
120+
show-trace: ## show trace
121+
uv run playwright show-trace $(TRACE_ZIP)
122+
123+
.PHONY: codegen
124+
codegen: ## generate test code
125+
uv run playwright codegen \
126+
--output tests/test_codegen.py

docs/index.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
11
# workshop-playwright-python
2+
3+
## Installation
4+
5+
To set up the environment, details are provided in [Playwright for Python > Installation](https://playwright.dev/python/docs/intro).
6+
In this workshop, we provide a script to install the necessary dependencies.
7+
8+
```bash
9+
git clone https://github.com/ks6088ts-labs/workshop-playwright-python.git
10+
cd workshop-playwright-python
11+
12+
# Install the dependencies
13+
make install-deps-dev
14+
```
15+
16+
- [[BUG] Host system is missing dependencies to run browsers (WSL2) #19100](https://github.com/microsoft/playwright/issues/19100)
17+
18+
## Guides
19+
20+
To run some demos, please follow the instructions below.
21+
22+
```bash
23+
# Run tests in verbose mode
24+
make test-verbose
25+
26+
# Show traces
27+
make show-trace
28+
29+
# Generate code
30+
make codegen
31+
```
32+
33+
## [Microsoft Playwright Testing](https://learn.microsoft.com/ja-jp/azure/playwright-testing/)
34+
35+
- [Get Started Sample](https://github.com/microsoft/playwright-testing-service/tree/main/samples/get-started)
36+
37+
## [Playwright MCP server](https://github.com/microsoft/playwright-mcp)

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ version = "0.0.1"
44
description = "A GitHub template repository for Python"
55
readme = "README.md"
66
requires-python = ">=3.10"
7-
dependencies = []
7+
dependencies = [
8+
"playwright>=1.52.0",
9+
"pytest-playwright>=0.7.0",
10+
]
811

912
[project.optional-dependencies]
1013
docs = [

tests/test_core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import logging
2+
import re
3+
4+
from playwright.sync_api import Page, expect
25

36
from workshop_playwright_python.core import hello_world
47

@@ -13,3 +16,20 @@ def test_hello_world_non_verbose(caplog):
1316
with caplog.at_level(logging.DEBUG):
1417
hello_world(verbose=False)
1518
assert "Hello, world!" not in caplog.text
19+
20+
21+
def test_has_title(page: Page):
22+
page.goto("https://playwright.dev/")
23+
24+
# Expect a title "to contain" a substring.
25+
expect(page).to_have_title(re.compile("Playwright"))
26+
27+
28+
def test_get_started_link(page: Page):
29+
page.goto("https://playwright.dev/")
30+
31+
# Click the get started link.
32+
page.get_by_role("link", name="Get started").click()
33+
34+
# Expects page to have a heading with the name of Installation.
35+
expect(page.get_by_role("heading", name="Installation")).to_be_visible()

tests/test_hatena_bookmark.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json
2+
3+
from playwright.sync_api import Page
4+
5+
6+
def test_hatena_bookmark(page: Page):
7+
page.goto("https://b.hatena.ne.jp/hotentry/all")
8+
9+
entries = []
10+
for entry in page.query_selector_all(".entrylist-contents-main"):
11+
title_element = entry.query_selector(".entrylist-contents-title a")
12+
bookmark_element = entry.query_selector(".entrylist-contents-users span")
13+
14+
if title_element and bookmark_element:
15+
title = title_element.inner_text()
16+
url = title_element.get_attribute("href")
17+
bookmarks = bookmark_element.inner_text().replace("users", "").strip()
18+
19+
entries.append({"title": title, "url": url, "bookmarks": int(bookmarks) if bookmarks.isdigit() else 0})
20+
21+
print(json.dumps(entries, indent=2, ensure_ascii=False))

0 commit comments

Comments
 (0)