From 4ff087d0be0bd0319772b44879b7ecd89ebbeb1f Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 1/8] 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 88a7e4f73b..5a3966998d 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 0000000000..1a4b15330a --- /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 0000000000..f1cea1d8e1 --- /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 0000000000..c197f1ae50 --- /dev/null +++ b/tests/playwright/requirements.txt @@ -0,0 +1,3 @@ +pytest +pytest-playwright +pytest-reporter-html1 From 20707ccdcffe45e41ae829663678dd03d0d03dca Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 2/8] 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 0000000000..dd2e067611 --- /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 0000000000..51b6ce9672 --- /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 9c2a332da53149c533d56e693d63fcfdc2f30ce3 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 3/8] 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 df1be87001..f3971f1de4 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 7d95242b540688ecc8d7c807773962e7a30c32ad Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 4/8] 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 0000000000..03d2265ad3 --- /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 dd2e067611..d390fc1e27 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 4004ea9eba80b701ce663b0970ae45b89a5ccaa8 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 22:57:50 +0000 Subject: [PATCH 5/8] 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 8d06749e6a..fe384aaca8 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 133981b8528507ba6644f98e4ffcbafc7fdb2290 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Mon, 2 Dec 2024 23:10:53 +0000 Subject: [PATCH 6/8] 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 5a3966998d..396d8a92a7 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 From 20515a814e175ec2080baa5db3e108be939e88d7 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Fri, 3 Jan 2025 23:47:30 +0000 Subject: [PATCH 7/8] chore: pin version of Playwright, set up dependabot --- .github/dependabot.yml | 18 ++++++++++++++++++ tests/playwright/requirements.txt | 1 + 2 files changed, 19 insertions(+) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 563c6a3333..a43abd21ee 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -25,3 +25,21 @@ updates: include: "scope" labels: - "dependencies" + - package-ecosystem: "docker" + directory: "/tests/playwright" # Playwright Docker image + schedule: + interval: "daily" + commit-message: + prefix: "chore" + include: "scope" + labels: + - "dependencies" + - package-ecosystem: "pip" + directory: "/tests/playwright" # Playwright pytest plugin + schedule: + interval: "daily" + commit-message: + prefix: "chore" + include: "scope" + labels: + - "dependencies" diff --git a/tests/playwright/requirements.txt b/tests/playwright/requirements.txt index c197f1ae50..36aad0ab09 100644 --- a/tests/playwright/requirements.txt +++ b/tests/playwright/requirements.txt @@ -1,3 +1,4 @@ +playwright==1.48.0 # needs to match version on Docker image pytest pytest-playwright pytest-reporter-html1 From efcf70938cc4da15c3c4de29a97f8f1223420952 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Wed, 5 Feb 2025 18:18:48 +0000 Subject: [PATCH 8/8] docs: start section for Playwright under automated testing docs --- docs/tests/automated-tests.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/tests/automated-tests.md b/docs/tests/automated-tests.md index 94701dc1b1..1c57beea35 100644 --- a/docs/tests/automated-tests.md +++ b/docs/tests/automated-tests.md @@ -23,3 +23,13 @@ The report files include a local `.gitignore` file, so the entire directory is h ### Latest coverage report We also make the latest (from `main`) coverage report available online here: [Coverage report](../coverage) + +## Playwright + +For testing the app flows from beginning to end, we use Playwright. + +To run all Playwright tests locally, open a terminal outside the devcontainer and run: + +```bash +docker compose run --rm playwright +```