[#646] Added focus assertions to 'ElementTrait'.#650
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
WalkthroughThis PR adds keyboard focus and visible focus outline assertions to ChangesFocus Assertions for ElementTrait
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #650 +/- ##
==========================================
+ Coverage 96.54% 96.57% +0.03%
==========================================
Files 44 44
Lines 3356 3389 +33
==========================================
+ Hits 3240 3273 +33
Misses 116 116 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Closes #646
Summary
Added four focus-related assertions to
ElementTrait: two for keyboard focus (should have keyboard focus/should not have keyboard focus) and two for visible focus indicators (should have a visible focus outline/should not have a visible focus outline). The keyboard-focus check tests whether an element is the currentdocument.activeElement; the outline check inspects computedoutline-style,outline-width, andbox-shadowto catch WCAG 2.4.7 (Focus Visible) regressions. Both pairs delegate to a shared protected helper with an$is_invertedflag, following the same DRY pattern used byelementAssertAttributeWithValue. Three fixture buttons were added toelements.htmlto exercise all three CSS states (outline, box-shadow, none), and ten new Behat scenarios cover every positive path and error branch.Changes
src/ElementTrait.phpelementAssertHasKeyboardFocus()andelementAssertNotHasKeyboardFocus()— public step methods that delegate toelementAssertKeyboardFocus().elementAssertHasVisibleFocusOutline()andelementAssertNotHasVisibleFocusOutline()— public step methods that delegate toelementAssertVisibleFocusOutline().elementAssertKeyboardFocus()— protected helper; returns__OK__,__NONE__, or the focused element's outer HTML to build context-rich failure messages.elementAssertVisibleFocusOutline()— protected helper; readsoutline-style,outline-width, andbox-shadowviagetComputedStyleand considers an element visible when either the outline or box-shadow is non-none.tests/behat/fixtures/elements.html#focus-button-outline(CSS outline),#focus-button-shadow(box-shadow indicator), and#focus-button-no-outline(no visible indicator) to the Focus Testing section.tests/behat/features/element.feature@javascriptpositive scenarios covering focus detection, outline detection, and box-shadow detection.@trait:ElementTraitnegative scenarios covering: missing element (both step types), wrong element focused, no element focused, element unexpectedly focused, missing element for outline, no outline when expected, outline present when not expected.STEPS.mdBefore / After
Summary
Added four public step methods to
ElementTraitfor asserting keyboard focus and visible focus outline states on DOM elements. These steps provide WCAG 2.4.7 (Focus Visible) validation capabilities to prevent accidentaloutline: noneregressions.Changes
Core Implementation (
src/ElementTrait.php, +152 lines)Public step methods:
elementAssertHasKeyboardFocus(string $selector)— Step:Then the element :selector should have keyboard focuselementAssertNotHasKeyboardFocus(string $selector)— Step:Then the element :selector should not have keyboard focuselementAssertHasVisibleFocusOutline(string $selector)— Step:Then the element :selector should have a visible focus outlineelementAssertNotHasVisibleFocusOutline(string $selector)— Step:Then the element :selector should not have a visible focus outlineProtected helper methods:
elementAssertKeyboardFocus(string $selector, bool $is_inverted)— Verifies element matchesdocument.activeElement. Returns__OK__,__NONE__(no focused element), or focused element's truncated outerHTML (first 200 chars) for failure messages. ThrowsElementNotFoundExceptionif selector matches no elements;ExpectationExceptionon assertion failure.elementAssertVisibleFocusOutline(string $selector, bool $is_inverted)— Checks computed styleoutline-style,outline-width, andbox-shadow. Considers element visible when outline is non-nonewith width > 0, or box-shadow is non-none. Throws same exceptions as keyboard focus helper.All methods follow step definition guidelines: use tuple format (
#[Then(...)]), start withelement, includeAssertprefix, useshould/should notin step text, and start step with entity being asserted.Feature Tests (
tests/behat/features/element.feature, +128 lines)Positive scenarios (
@javascript):Negative scenarios (
@trait:ElementTrait):Test Fixtures (
tests/behat/fixtures/elements.html, +3 lines)Extended Focus Testing section with three button elements:
#focus-button-outline— inline style withoutline: 2px solid blue#focus-button-shadow— inline style withbox-shadow: 0 0 0 2px rgb(0, 0, 255)andoutline: none#focus-button-no-outline— inline style with bothoutline: noneandbox-shadow: noneDocumentation (
STEPS.md, +56 lines)Updated ElementTrait section to document the four new step definitions with parameter and execution context details.
Compliance
All step definitions follow CONTRIBUTING.md guidelines:
element) and includeAssertprefixshould/should notand starts with entity being assertedelementExecuteJs()infrastructureElementNotFoundExceptionfor missing selectors,ExpectationExceptionfor assertion failures:selector) used consistently