-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add Axe/Playwright tests #165
Conversation
bd7a73d
to
e4ff98d
Compare
bf799be
to
912aea2
Compare
423fbce
to
ec5de06
Compare
ec5de06
to
9fe5a59
Compare
os.chdir('..') | ||
command = ["poetry", "run", "python", "manage.py", "show_magiclink_url", email_address] | ||
result = subprocess.run(command, capture_output=True, text=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subprocess.run
takes cwd
argument you can use to set the working dir. Using os.chdir
also feels a little worrying to me as we already have a repo where people assume the working directory is two different places depending on whether you're front or backend. Suggest using Path(__file__)
to establish this file location as a baseline and navigate from there, so like django_root = Path(__file__).parents[2]
for the Django app root, I think. Can even assert (django_root / "manage.py").exists()
to make sure it's working before running subprocesses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the working dir for this is tests_playwright
it felt safest to navigate one step back from there, rather than outwards in (if that makes sense) - i.e. tests_playwright
will always be an immediate child dir of django_app
unless we explicitly move these tests. And so it should work regardless of what the overall working dir is. Unless I've missed something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.chdir()
will work relative to the current working directory, right? Run pytest --collect-only
from root -- your playwright tests will come up in the list of tests to run. I'm not certain, but if I ran them from there the working directory would be root, not the directory of this test, right?
In short, I think you can't guarantee the user's working directory when they run this, but you can guarantee where the file is saved, so use that to define the working directory for subprocess.run
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, with you, thanks @wpfl-dbt. That's now updated!
|
||
## Setup | ||
|
||
`poetry run playwright install` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's hard to know if it's just my local being a bit out of date but as I ran poetry install
before running this command I got told the lockfile within the Django app was out of date. Might want to check at your end with poetry lock --no-update
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now updated the poetry.lock
file
|
||
The tests are currently located at `django_app/tests_playwright`. All commands below should be run from this directory. | ||
|
||
## Setup |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all assumed the django app is running locally... on 8090
. This isn't in the app documentation, which will bring it up on 8000
. Suggest adding...
docker-compose up -d db minio
poetry run python manage.py runserver 8090
...to your instructions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point @wpfl-dbt.
I've now added a _settings.py
file with BASE_URL and added this as a setup step in the README.
|
||
# === Playwright Tests === | ||
|
||
USER_EMAIL= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't got tests working yet but assuming this can't be left blank, would strongly suggest giving it a sensible default, like "redbox-copilot@cabinetoffice.gov.uk", an email address already in the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't be left blank, but adding a default won't work as it needs to be whatever user you have setup. I've taken a couple of extra steps now:
- Make the instruction in the README clearer
- Throw an error as soon as possible if
USER_EMAIL
not set with a clear explanation of how to fix
import os | ||
import subprocess | ||
from playwright.sync_api import Page, expect | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This didn't work for me at all until I forcibly loaded the environment variables.
from dotenv import load_dotenv
from pathlib import Path
ROOT = Path(__file__).parents[1]
load_dotenv(dotenv_path=ROOT / ".env", override=True)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added that in - thank you
|
||
axe = Axe() | ||
|
||
def test_violations(page: Page): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even need to change anything to see this works -- /documents
throws a bunch of accessibility issues for me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, it needed a migration. This isn't specific to this work, but I'll add a note to the main PR text in case anyone else also reviews!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite working well enough to approve, I think. I've left a bunch of comments.
Thanks @wpfl-dbt, these comments are super helpful. I'm working through them now, and will reply to each one individually. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy for this to go in once merge conflicts are resolved.
Might also be worth bringing in the latest linting rules from pyproject.toml
and running them.
Add Poetry dependencies
ec24f6b
to
3fca7f9
Compare
Context
This is setting up Playwright for accessibility and end-to-end testing, using the pytest-playwright package. The accessibility tests use Axe.
The primary focus was accessibility testing, but this also serves as an MVP for additional end-to-end testing in future.
Setup
Follow the instructions in the README at
django_app/tests_playwright/
.If any pages aren't working on this branch (.e.g. /documents) you may need to run
poetry run python manage.py migrate
.How to test
First check that all the tests pass
Then create some accessibility issues on a page. Here's a few if you're struggling for ideas:
And check the errors are reported