Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Playwright setup #2500

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Feat: Playwright setup #2500

wants to merge 6 commits into from

Conversation

angela-tran
Copy link
Member

@angela-tran angela-tran commented Nov 4, 2024

This PR adds a service called playwright to compose.yml. Developers can use this container to run Playwright tests.

The base image of this service is a Microsoft-built image which includes Playwright and its dependencies already installed, thereby avoiding a long installation step for those.

This PR also includes a working test for running through the CST agency card flow on dev-benefits.calitp.org. You'll notice that we do install pytest-playwright into the devcontainer (line 41 of pyproject.toml) so that when viewing the test code in the devcontainer, we still have a nice developer experience.

Just to make this super clear: you should write Playwright tests in the devcontainer just as you would with writing application code and unit tests, but run Playwright tests from the playwright service container. The devcontainer does not have Playwright's dependencies and browsers installed in it.

Trying it out

(this is all outside of the devcontainer)

First, create a playwright/.env file:

cd playwright
cp .env.sample .env
cd ..

Start the playwright service by running:

docker compose up playwright -d

and then open a terminal session in it by running:

docker exec -ti benefits-playwright-1 /bin/bash

where benefits-playwright-1 is the name of the container you started with the first command.

Set values you want the tests to use in playwright/.env, and then use the helper script to run the Playwright tests:

(The tests will run as headless.)

./run.sh

The console output will hopefully tell you the tests passed, and you can view more output under playwright/test-results.

Small note about the test

I'm still learning the Playwright API, so note that there may be a better way to do what I'm doing with wait_for_timeout.

Helpful docs

use a base image that already has playwright and its browsers installed
add some test stubs, with one test that is a work-in-progress.

the devcontainer will have pytest-playwright installed so that when
we're writing tests, we can use auto-complete, reference code definition,
etc.

use the 'playwright' service container to run the tests. there is a sample
.env file and a helper script for running tests.
this makes it so they are used any time pytest is invoked from this
directory
this implements it in a simple way that works. there are probably
improvements that can be made, e.g. not waiting 10 seconds for the
enrollment to finish.

mark medicaregov flow test to be skipped.
since the tests-pytest workflow runs from the root level, it was also
collecting Playwright tests.
the tests-cypress workflow was failing because there is no .env file
for the playwright service, but we don't even need that service to
start.

this makes it so it is not started by default.
see https://docs.docker.com/compose/how-tos/profiles/ for more.
@angela-tran angela-tran self-assigned this Nov 4, 2024
@angela-tran angela-tran requested a review from a team as a code owner November 4, 2024 18:38
@github-actions github-actions bot added deployment-dev [auto] Changes that will trigger a deploy if merged to dev tests Related to automated testing (unit, UI, integration, etc.) docker Application container, devcontainer, Compose, etc. and removed deployment-dev [auto] Changes that will trigger a deploy if merged to dev labels Nov 4, 2024
Copy link

github-actions bot commented Nov 4, 2024

Coverage report

This PR does not seem to contain any modification to coverable code.

@angela-tran
Copy link
Member Author

I just realized / re-remembered that in the file structure, we have

tests/cypress
tests/pytest

so if it feels better to move everything in playwright in this PR to instead be under

tests/playwright

I could do that.

@thekaveman
Copy link
Member

It would be nice if you could simply run:

docker compose run playwright

And have that run the tests and output the results. In other words, overriding the entrypoint/cmd to call the run.sh script you created.



def test_agency_card_flow(page: Page):
page.goto("https://dev-benefits.calitp.org/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you meant page.goto("/")?


set -e

base_url="https://dev-benefits.calitp.org"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe take this from the command line?

@angela-tran angela-tran marked this pull request as draft November 5, 2024 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker Application container, devcontainer, Compose, etc. tests Related to automated testing (unit, UI, integration, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants