Problem/Motivation
In working on Media Library, we paid a lot of attention to accessibility issues and fixed many problems in that space. We also tested our fixes. Many of the problems (and fixes) were related to where the focus goes while the user interacts with the UI. To facilitate more accessibility testing in JavaScript tests, it would be great if we had a way to assert that a particular element has, or does not have, focus.
Proposed resolution
Add two methods to JsWebAssert: assertElementHasFocus() and assertElementNotHasFocus().
Remaining tasks
Implement it, review it, commit it, and start using it!
User interface changes
None.
API changes
JsWebAssert, and therefore all JavaScript tests, will receive new assertion methods.
Data model changes
None.
Release notes snippet
TBD
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 3041768-9.patch | 3.75 KB | krzysztof domański |
| #9 | interdiff-8-9.txt | 1.17 KB | krzysztof domański |
| #8 | 3041768-8.patch | 3.75 KB | krzysztof domański |
| #8 | interdiff-7-8.txt | 2.51 KB | krzysztof domański |
| #7 | interdiff-3041768-5-7.txt | 1.32 KB | phenaproxima |
Issue fork drupal-3041768
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 #2
phenaproximaA first thing for interested parties to look at. Still needs explicit tests.
Rather than using jQuery, which is not loaded by default on all pages, this uses the
document.activeElementproperty, which looks to have wide browser support: https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/ac...Comment #3
wim leersTotally makes sense to me! +1 for concept. And thanks for doing this! 🙏
s/Tests that/Asserts if/
\Behat\Mink\WebAssert::elementExistslists other possible values. Or is this case-insensitive? :OIt'd be better to set
NULLas the default, so that we can detectif (is_null($message)) {…}and then generate a more meaningful error message, one that also conveys the used selector. This results in a better DX.Must be third person singular.
I wonder if
elementHasFocus()would be more appropriate?Woah, what?!
Could use docs.
Comment #4
seanbThis looks super useful! Can we add a container parameter to these methods? A selector could not be unique in the page, while it is unique in a container.
Comment #5
phenaproxima#3:
Also addressed #4.
Comment #6
lendudeSince the message being set here is the message in the exception when the assert fails, I would expect the messages to be the other way around
Comment #7
phenaproximaD'oh! Good point. Fixed!
Comment #8
krzysztof domański1. Improving PHPDoc commens.
2. Most methods of the WebAssert class do return the found elements or there is a plan to do it. Let's do the same in this case.
#2913411: Make methods in WebAssert return matching elements.
Comment #9
krzysztof domańskiRe #3.2
vendor/behat/mink/src/Exception/ElementNotFoundException.phpusing lowercase element selector type so this is case sensitive.Comment #10
lauriii+1 to adding assertions for this! 🔥
To avoid false positives, it makes sense that we assert whether the specific element is focused, not taking into account that children could be focused. We should document this explicitly in the doc block to avoid confusion.
Comment #11
krzysztof domańskiFollow-up #3.2 and #9 I created quick fix.
#3041900: The element selector type "CSS, XPath" in JSWebAssert should be lowercase
It can be confusing so we should change it in other places.
Comment #19
smustgrave commented+1 for this!
Sounds like there are small changes needed for #10 possibly #11.
Comment #21
mgiffordAnother approach for this is described here https://opensource.com/article/23/2/automated-accessibility-testing
by using https://github.com/dmtrKovalenko/cypress-real-events
Not sure if there is a similar library for Nightwatch or if JsWebAssert is essentially equivalent.