From 5acf79a50d35708b6aea09aad2dcbd8ff8eb82c5 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 1/6] feat: add setup for a separate container to run playwright use a base image that already has playwright and its browsers installed --- compose.yml | 10 ++++++++++ tests/playwright/.gitignore | 3 +++ tests/playwright/Dockerfile | 11 +++++++++++ tests/playwright/requirements.txt | 3 +++ 4 files changed, 27 insertions(+) create mode 100644 tests/playwright/.gitignore create mode 100644 tests/playwright/Dockerfile create mode 100644 tests/playwright/requirements.txt diff --git a/compose.yml b/compose.yml index c1a400b30..9bb010f5a 100644 --- a/compose.yml +++ b/compose.yml @@ -41,3 +41,13 @@ services: - "8000" volumes: - ./.devcontainer:/.devcontainer + + playwright: + build: + context: . + dockerfile: tests/playwright/Dockerfile + image: benefits_client:playwright + volumes: + - ./tests/playwright:/playwright + # https://code.visualstudio.com/docs/remote/create-dev-container#_use-docker-compose + entrypoint: sleep infinity diff --git a/tests/playwright/.gitignore b/tests/playwright/.gitignore new file mode 100644 index 000000000..1a4b15330 --- /dev/null +++ b/tests/playwright/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +.pytest_cache +test-results diff --git a/tests/playwright/Dockerfile b/tests/playwright/Dockerfile new file mode 100644 index 000000000..f1cea1d8e --- /dev/null +++ b/tests/playwright/Dockerfile @@ -0,0 +1,11 @@ +# https://playwright.dev/docs/docker +FROM mcr.microsoft.com/playwright/python:v1.48.0-jammy + +WORKDIR /playwright + +COPY tests/playwright/requirements.txt requirements.txt + +RUN python -m pip install --upgrade pip && \ + pip install -r requirements.txt + +USER pwuser diff --git a/tests/playwright/requirements.txt b/tests/playwright/requirements.txt new file mode 100644 index 000000000..c197f1ae5 --- /dev/null +++ b/tests/playwright/requirements.txt @@ -0,0 +1,3 @@ +pytest +pytest-playwright +pytest-reporter-html1 From 6bb718f18e6e120c7831bed2ca3b8c9c01ee7552 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 2/6] feat(playwright): add healthcheck test and helper script for running --- tests/playwright/run.sh | 5 +++++ tests/playwright/test_healthcheck.py | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100755 tests/playwright/run.sh create mode 100644 tests/playwright/test_healthcheck.py diff --git a/tests/playwright/run.sh b/tests/playwright/run.sh new file mode 100755 index 000000000..dd2e06761 --- /dev/null +++ b/tests/playwright/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -e + +pytest --tracing on -v --template=html1/index.html --report=test-results/report.html --video on diff --git a/tests/playwright/test_healthcheck.py b/tests/playwright/test_healthcheck.py new file mode 100644 index 000000000..51b6ce967 --- /dev/null +++ b/tests/playwright/test_healthcheck.py @@ -0,0 +1,7 @@ +from playwright.sync_api import Page, expect + + +def test_dev_healthcheck(page: Page): + page.goto("https://dev-benefits.calitp.org/healthcheck") + + expect(page.get_by_text("Healthy")).to_be_visible() From 620c0fe26a0a2895b9fffb1014283cadd248873f Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 3/6] chore: add pytest-playwright to devcontainer this provides a better dev experience when reading and writing tests from within the devcontainer --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index a2b2080fc..52845ef34 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,7 @@ test = [ "pytest", "pytest-django", "pytest-mock", + "pytest-playwright", # only for writing tests. run tests using playwright Docker Compose service "pytest-socket", ] From 893afe8090298894263218fae61cd0fbd4f474ff Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 4/6] refactor: extract some helper script args out into pytest.ini file this makes it so they are used any time pytest is invoked from this directory --- tests/playwright/pytest.ini | 2 ++ tests/playwright/run.sh | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 tests/playwright/pytest.ini diff --git a/tests/playwright/pytest.ini b/tests/playwright/pytest.ini new file mode 100644 index 000000000..03d2265ad --- /dev/null +++ b/tests/playwright/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +addopts = --tracing on -v --template=html1/index.html --report=test-results/report.html --video on diff --git a/tests/playwright/run.sh b/tests/playwright/run.sh index dd2e06761..d390fc1e2 100755 --- a/tests/playwright/run.sh +++ b/tests/playwright/run.sh @@ -2,4 +2,4 @@ set -e -pytest --tracing on -v --template=html1/index.html --report=test-results/report.html --video on +pytest From ec0ed68464b50ba7f93f06aef33b795f523af9c4 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 5/6] fix(ci): specify directory to use for running unit tests since the tests-pytest workflow runs from the root level, it will also collect Playwright tests if we don't specify the directory. --- tests/pytest/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pytest/run.sh b/tests/pytest/run.sh index 8d06749e6..fe384aaca 100755 --- a/tests/pytest/run.sh +++ b/tests/pytest/run.sh @@ -2,7 +2,7 @@ set -eu # run normal pytests -coverage run -m pytest +coverage run -m pytest tests/pytest # clean out old coverage results rm -rf benefits/static/coverage From 45de26864d22848b4825d3a4e8d15e405e0398ea Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 23:10:53 +0000 Subject: [PATCH 6/6] refactor: make playwright service's default command be to run the tests --- compose.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compose.yml b/compose.yml index 9bb010f5a..ae0c06979 100644 --- a/compose.yml +++ b/compose.yml @@ -49,5 +49,4 @@ services: image: benefits_client:playwright volumes: - ./tests/playwright:/playwright - # https://code.visualstudio.com/docs/remote/create-dev-container#_use-docker-compose - entrypoint: sleep infinity + command: ./run.sh