Skip to content

Commit 0f7f526

Browse files
authored
Generate screenshot when "design_change" label is used (#483)
* create add images for files * rename playwright test * update imports to fix tests * update compression * change path * add jpg suffix to file
1 parent 09108fe commit 0f7f526

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Generate and Upload Screenshots
2+
# https://devblogs.microsoft.com/python/announcing-playwright-for-python-reliable-end-to-end-testing-for-the-web/
3+
# https://github.com/marketplace/actions/run-playwright-tests
4+
5+
on:
6+
pull_request:
7+
types: [labeled]
8+
branches:
9+
- gh-pages
10+
11+
workflow_dispatch:
12+
13+
jobs:
14+
test:
15+
if: ${{ github.event.label.name == 'design_change' }}
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: "3.12"
23+
- name: Install dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install -r requirements-dev.txt
27+
python -m playwright install --with-deps chromium
28+
- name: Set up Ruby
29+
uses: ruby/setup-ruby@v1
30+
with:
31+
bundler-cache: true
32+
# Run tests
33+
- name: Run Tests
34+
run: |
35+
python -m pytest -m design
36+
- name: upload images
37+
uses: actions/upload-artifact@v4
38+
with:
39+
name: page_screenshots
40+
path: test_images/
41+
compression-level: 9

pytest.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
[pytest]
22
python_files = tests/*.py
3-
addopts = -v
3+
addopts = -v -m "not design"
4+
markers =
5+
design: get visuals for test paths

tests/test_design.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import pathlib
2+
3+
import pytest
4+
from playwright.sync_api import Page, sync_playwright
5+
from xprocess import ProcessStarter
6+
7+
8+
@pytest.fixture(scope="module")
9+
def per_device_page_url(xprocess, loaded_profile, url_port):
10+
"""Returns the url of the live server"""
11+
12+
url, port = url_port
13+
14+
class Starter(ProcessStarter):
15+
# Start the process
16+
args = [
17+
"bundle",
18+
"exec",
19+
"jekyll",
20+
"serve",
21+
"--source",
22+
pathlib.Path().cwd().absolute(),
23+
"--port",
24+
port,
25+
]
26+
terminate_on_interrupt = True
27+
pattern = "Server running... press ctrl-c to stop."
28+
29+
xprocess.ensure("per_device_page_url", Starter)
30+
31+
with sync_playwright() as p:
32+
device = p.devices[loaded_profile]
33+
browser = p.chromium.launch()
34+
context = browser.new_context(**device)
35+
page = context.new_page()
36+
37+
# Return the URL of the live server
38+
yield page, url
39+
40+
# Clean up the process
41+
xprocess.getinfo("per_device_page_url").terminate()
42+
43+
44+
@pytest.fixture(scope="session")
45+
def create_test_image():
46+
image_path = pathlib.Path("./").joinpath("test_images")
47+
48+
if not image_path.is_dir():
49+
image_path.mkdir()
50+
return image_path
51+
52+
53+
@pytest.mark.design
54+
def test_route_designs(
55+
loaded_route: str,
56+
per_device_page_url: tuple[Page, str],
57+
create_test_image,
58+
request,
59+
) -> None:
60+
"""Test that the destinations page loads with seeded data"""
61+
# Create a destination
62+
page, live_server_url = per_device_page_url
63+
response = page.goto(f"{live_server_url}/{loaded_route}")
64+
page.screenshot(
65+
path=create_test_image.joinpath(request.node.name).with_suffix(".jpg"),
66+
full_page=True,
67+
)
68+
assert response.status == 200

0 commit comments

Comments
 (0)