Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,5 @@ cython_debug/
# Project
*.env
requirements.txt
generated
tests/test_codegen.py
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ info: ## show information
install-deps-dev: ## install dependencies for development
uv sync --all-extras
uv run pre-commit install
uv run playwright install

.PHONY: install-deps
install-deps: ## install dependencies for production
Expand Down Expand Up @@ -98,3 +99,28 @@ docs-serve: ## serve documentation

.PHONY: ci-test-docs
ci-test-docs: docs ## run CI test for documentation

# ---
# Project
# ---

.PHONY: test-verbose
test-verbose: ## run tests with verbose
uv run pytest \
--capture=no \
--verbose \
--headed \
--tracing on \
--video on \
--screenshot on \
--output generated

TRACE_ZIP ?= generated/tests-test-core-py-test-get-started-link-chromium/trace.zip
.PHONY: show-trace
show-trace: ## show trace
uv run playwright show-trace $(TRACE_ZIP)

.PHONY: codegen
codegen: ## generate test code
uv run playwright codegen \
--output tests/test_codegen.py
36 changes: 36 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
# workshop-playwright-python

## Installation

To set up the environment, details are provided in [Playwright for Python > Installation](https://playwright.dev/python/docs/intro).
In this workshop, we provide a script to install the necessary dependencies.

```bash
git clone https://github.com/ks6088ts-labs/workshop-playwright-python.git
cd workshop-playwright-python

# Install the dependencies
make install-deps-dev
```

- [[BUG] Host system is missing dependencies to run browsers (WSL2) #19100](https://github.com/microsoft/playwright/issues/19100)

## Guides

To run some demos, please follow the instructions below.

```bash
# Run tests in verbose mode
make test-verbose

# Show traces
make show-trace

# Generate code
make codegen
```

## [Microsoft Playwright Testing](https://learn.microsoft.com/ja-jp/azure/playwright-testing/)

- [Get Started Sample](https://github.com/microsoft/playwright-testing-service/tree/main/samples/get-started)

## [Playwright MCP server](https://github.com/microsoft/playwright-mcp)
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version = "0.0.1"
description = "A GitHub template repository for Python"
readme = "README.md"
requires-python = ">=3.10"
dependencies = []
dependencies = [
"playwright>=1.52.0",
"pytest-playwright>=0.7.0",
]

[project.optional-dependencies]
docs = [
Expand Down
20 changes: 20 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import logging
import re

from playwright.sync_api import Page, expect

from workshop_playwright_python.core import hello_world

Expand All @@ -13,3 +16,20 @@ def test_hello_world_non_verbose(caplog):
with caplog.at_level(logging.DEBUG):
hello_world(verbose=False)
assert "Hello, world!" not in caplog.text


def test_has_title(page: Page):
page.goto("https://playwright.dev/")

# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))


def test_get_started_link(page: Page):
page.goto("https://playwright.dev/")

# Click the get started link.
page.get_by_role("link", name="Get started").click()

# Expects page to have a heading with the name of Installation.
expect(page.get_by_role("heading", name="Installation")).to_be_visible()
21 changes: 21 additions & 0 deletions tests/test_hatena_bookmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json

from playwright.sync_api import Page


def test_hatena_bookmark(page: Page):
page.goto("https://b.hatena.ne.jp/hotentry/all")

entries = []
for entry in page.query_selector_all(".entrylist-contents-main"):
title_element = entry.query_selector(".entrylist-contents-title a")
bookmark_element = entry.query_selector(".entrylist-contents-users span")

if title_element and bookmark_element:
title = title_element.inner_text()
url = title_element.get_attribute("href")
bookmarks = bookmark_element.inner_text().replace("users", "").strip()

entries.append({"title": title, "url": url, "bookmarks": int(bookmarks) if bookmarks.isdigit() else 0})

print(json.dumps(entries, indent=2, ensure_ascii=False))
Loading