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

Add Axe/Playwright tests #165

Merged
merged 8 commits into from
May 23, 2024
Merged

Add Axe/Playwright tests #165

merged 8 commits into from
May 23, 2024

Conversation

KevinEtchells
Copy link
Contributor

@KevinEtchells KevinEtchells commented Mar 28, 2024

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

  1. First check that all the tests pass

  2. Then create some accessibility issues on a page. Here's a few if you're struggling for ideas:

<h2>Main page heading, instead of a h1</h2>
<a href=""></a>
<img src="broken-and-no-alt.jpg"/>

And check the errors are reported

@KevinEtchells KevinEtchells changed the title Add Axe/Playwright tests Draft: Add Axe/Playwright tests Apr 22, 2024
@KevinEtchells KevinEtchells force-pushed the feature/accessibility-testing branch from bd7a73d to e4ff98d Compare May 2, 2024 10:29
@KevinEtchells KevinEtchells force-pushed the feature/accessibility-testing branch from bf799be to 912aea2 Compare May 22, 2024 07:58
@KevinEtchells KevinEtchells force-pushed the feature/accessibility-testing branch from 423fbce to ec5de06 Compare May 22, 2024 11:00
@KevinEtchells KevinEtchells force-pushed the feature/accessibility-testing branch from ec5de06 to 9fe5a59 Compare May 22, 2024 11:04
@KevinEtchells KevinEtchells marked this pull request as ready for review May 22, 2024 11:04
@KevinEtchells KevinEtchells changed the title Draft: Add Axe/Playwright tests Add Axe/Playwright tests May 22, 2024
Comment on lines 18 to 20
os.chdir('..')
command = ["poetry", "run", "python", "manage.py", "show_magiclink_url", email_address]
result = subprocess.run(command, capture_output=True, text=True)
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

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`
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Collaborator

@wpfl-dbt wpfl-dbt May 23, 2024

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.

Copy link
Contributor Author

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=
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

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)

Copy link
Contributor Author

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):
Copy link
Collaborator

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!

Copy link
Contributor Author

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!

Copy link
Collaborator

@wpfl-dbt wpfl-dbt left a 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.

@KevinEtchells
Copy link
Contributor Author

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.

Copy link
Collaborator

@wpfl-dbt wpfl-dbt left a 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.

@KevinEtchells KevinEtchells force-pushed the feature/accessibility-testing branch from ec24f6b to 3fca7f9 Compare May 23, 2024 14:00
@KevinEtchells KevinEtchells merged commit 7f4b53b into main May 23, 2024
4 of 6 checks passed
@KevinEtchells KevinEtchells deleted the feature/accessibility-testing branch May 23, 2024 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants