Problem/Motivation
#3553673: Migrate from Nightwatch to Playwright (and phpunit Axe) tests will remove Nightwatch Axe tests from core.
This issue is to set up Axe testing in PHPUnit and migrate the tests.
Proposed resolution
- Create a PHP trait to inject Axe-core for tests and run it on the page.
- Create a new base class for
FunctionalJavascripttests that uses the trait and defines a test runner which developers can leverage by simply creating a data provider. - Update
.gitlab-ci.ymlto runyarn installbeforeFunctionalJavascripttests.
Proposed error message format
<BASE_URL> will be replaced with the actual test site URL.
Accessibility test failures (2 total)
1. [serious] Elements must meet minimum color contrast ratio thresholds
Test URL: <BASE_URL>/accessibility-test
Axe rule: `color-contrast`
Violating targets (1):
* `#target-low-contrast`
2. [critical] Form elements must have labels
Test URL: <BASE_URL>/accessibility-test
Axe rule: `label`
Violating targets (1):
* `input`
Remaining tasks
Resolve issue with javascript dependency.Update tests for any new cases that have been added.Remove existing Nightwatch a11y tests.Copy recent changes in the Navigation module Nightwatch a11y test to the correspondingAccessibilityTest.phpin this MR.- Decide on MVP for error messages.
- Decide what, if anything, to do with
incompleteresults. They are not currently included in the output, as axe-core separates them from violations.
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3338664
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
duaelfrMR open for thoughts and reviews
I added a very basic test on Olivero to ensure it works (I never added a dependency so I'm not sure).
Comment #4
duaelfrThis is related to #3293469: Automated A11y tests in Nightwatch
We might discuss if we want both to be integrated or not.
Comment #5
smustgrave commentedNot sure if this is a valid test but I reused MediaSourceImageTest::testMediaImageSource
Disabled required alt text
Commented out line $page->fillField("{$source_field_id}[0][alt]", ''); so the image had no alt text
Then ran
Expected it to fail but I didn't and just continued along.
Is there something I'm missing?
Comment #6
duaelfr@smustgrave Thanks for testing! An empty alt text is not a failure. What's forbidden is no
altattribute at all!It would be a failure if the image was the only content of a link for example.
Comment #7
mgiffordNow we have https://www.drupal.org/project/drupal/issues/3293469
Do we need this issue? Is PHPUnit going to give us different or better results?
Comment #9
duaelfrHi @mgifford!
On its current state, I believe that my patch is less interesting that Nightwatch's one because you can only ignore some problems based on their severity. I believe it could be improved by adding a way to alter the axe rules like it's done in JS. I also considered adding some ways to assert expected success or failures in a future version of my patch.
All that to say that I think the only advantage of this one instead of Nightwatch's one would be that some devs could be more comfortable using PHPUnit than Nigthwatch.
Do you think it could be interesting to have both available?
Comment #10
mgiffordThat I don't know. You've got the PHP developers & the JavaScript developers. Do you think PHPUnit is better supported by modules/themes than Nightwatch?
I think there's some split between nightwatch/cypress in the JS testing community. I'll ask around and see if we can get some other thoughts on this.
I'd suggest that we might want to test different things with them.
Comment #11
dmundraI think since axe-core package is now available in version 10.1.x (with the nightwatch update) I think a PHP example could be good to have but I am not sure if we need the same setup for an install profile and such.
Is the AccessibilityTest being picked up by the pipeline? I am not seeing it https://dispatcher.drupalci.org/job/drupal_patches/164094/console if it is.
Comment #12
kentr commentedI think it would be great if a11y tests ran automatically for most paths requested by a test. Currently, it appears that Axe tests in Nightwatch are only run for explicitly-defined paths in dedicated tests.
Based on limited experience with Nightwatch, setting up test conditions appears to be easier for PHPUnit functional javascript tests than it is for Nightwatch tests.
It also appears that there are currently more functional javascript tests than Nightwatch tests (at least, going by the number of test files turned up with a quick
find): 242 vs 34.Doing the a11y tests with PHPUnit might help cast the widest net.
Comment #13
catchComment #14
kentr commentedIn light of #3467492-15: [policy, no patch] Replace Nightwatch with Playwright, I'm working on this against
11.xand planning to add support for options so that we can use it like we use Nightwatch'saxeRun().Comment #16
kentr commentedI need implementation advice.
The axe core javascript is an npm package.
In a Slack discussion debugging the failed job, @fjgarlin said that core functional javascript tests don't have access to the
core/node_modulesdirectory. So, the file must be provided some other way.It has to be built and the dist files aren't in the repo, so it can't be retrieved directly from the repo.
I didn't find any other cases like this, so I experimented with installing it locally via Composer into
vendorby defining a repository incomposer.jsonas follows and then requiring it as a Composer dev dependency. This is working locally.Is this a viable solution, or are there other preferred solutions?
Comment #17
kentr commentedAdding to my last comment:
In case it's preferable to commit the axe javascript file into core, AFAICT it is Mozilla Public License 2.0 and is
541Kminified.Comment #19
kentr commentedHid the original MR because the new MR builds on it.
Comment #20
kentr commentedBumping to major due to @catch's comment #8 on #3542476-08: [Policy] Officially move to WCAG 2.2 AA Support.
Adding to #16:
AFAICT, to move this forward we just need to make a built version of the axe-core javascript available to these functional javascript tests.
It doesn't matter whether the script is stored in the repo, passed in as an artifact from another part of the gitlab build process, installed with
yarn installfor these tests, installed via php-forge/foxy, or something else.What is the desired mechanism for that?
Comment #21
catchWe'd normally add it as a JavaScript development dependency (same as cspell, eslint etc.) and then it will be installed in the general yarn install that CI pipelines already do. That way it's also available for people running the tests locally.
Comment #22
kentr commented@catch,
It's already a JavaScript dev dependency as a dependency of
nightwatch-axe-verbose, but it wasn't available to the tests.Here are snippets from
pipeline.yml.Based on your comment, I'll assume that it's OK to change
pipeline.ymland play around with it from there.Comment #23
smustgrave commentedNot sure if this plays into anything but know there's been talks for a little bit about removing nightwatch eventually.
Comment #24
kentr commented@smustgrave, this will add
axe-coreas a main dependency so thatnightwatch-axe-verboseisn't required anymore.It probably won't remove
nightwatch-axe-verbosethough, unless all of the existing Axe tests are also to be converted in this issue (rather than a followup).Comment #25
kentr commentedComment #27
the_g_bomb commentedI pulled the changes and started to test, all was looking good until I realised all I was doing was replicating what was already being done in the pipeline.
Comment #28
kentr commentedI realized that the Nightwatch Axe tests need to be removed. And when I reviewed them, I realized the breadcrumb case that was added for #3223147: Claro breadcrumb doesn't meet minimum target-size isn't represented here.
Also, the current Nightwatch a11y tests can switch themes based on CLI arguments. These replacement's don't.
It doesn't seem necessary right now, but when Gin comes to core it would be handy. I think it would be better in a follow-up issue.
Comment #29
kentr commentedOk, now I think it's ready.
I removed the existing Nightwatch a11y tests, but I left the yarn
nightwatch-axe-verbosedependency to make the transition smoother in case other tests are added in the near future.Comment #30
mstrelan commentedI think there is a bit of boilerplate we can remove, have marked up in the MR. Also wondering about a base class, see comment in the MR. These are not blockers though so I'd leave the status as NR, except I think the
$session->wait()needs attention, so setting to NW.Comment #31
kentr commentedLooking into the comments.
Comment #32
kentr commentedI made some changes based on @mstrelan's suggestions and raised some questions in comments on the MR.
Comment #33
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #34
kentr commentedUpdated the fork.
Comment #35
dcam commentedThis needs a rebase. Heads-up: you'll need to add the
#[RunTestsInSeparateProcesses]attribute to all of the new test classes when you do since they're all children of BrowserTestBase. I'm setting the status to Needs Work for that.I didn't do a completely thorough review of the MR, but I did read most of it. This is looking good so far. I resolved a couple of MR comment threads and left one comment reply.
Comment #36
kentr commentedI rebased the MR, added required attributes, and replied to @dcam's comment on the MR.
Pipeline is failing because
.with-composer-and-yarnwas removed frompipeline.yml.It needs some gitlab expertise. I made some failed attempts to follow the pattern used in
.gitlab-ci.ymlfor.yarn-install-from-cache.Comment #38
kentr commentedThe solution to getting
yarn installback for functional JS tests may be affected by #3572375: [ci] Run tests in top level pipeline, use recursion for secondary test runs.Comment #39
kentr commentedReady for another review.
Comment #40
kentr commentedAddendum FTR: The blocking issue #3572375: [ci] Run tests in top level pipeline, use recursion for secondary test runs was fixed.
Comment #41
mgiffordFixing bugs early reduces the costs in a project. With accessibility that this can scale up quickly. We do need to shift left to see that we're maximizing our text coverage as much as we can, while the ideas are still "fresh" in our heads. If we wait for a few months, or even longer for a user on a live site to report the bug it becomes much more expensive.
Early automated testing is key. Lets get this patch in!
Comment #42
kentr commentedLooks like there are merge conflicts.
Also, there were recent additions to the Navigation module's
a11yTest.jsthat need to be copied to the correspondingAccessibilityTest.phphere.Comment #43
smustgrave commentedSomething that's also needed in the summary is a dependency evaluation since this ticket is proposing a new package that would have to be kept up with by the team. So need to know things like how often it does release, number of open issues (any issues we should be concerned with), does it rely on any dependencies, etc.
Comment #44
kentr commented@smustgrave
Thanks for looking at this and changing the status. I should have changed that previously.
This is now using the Axe-core dependency that's already in core for Nightwatch. I updated the IS.
Comment #45
kentr commented