Skip to content

add data-testid hooks and aria-labels for future improvement - #2473

Merged
grzanka merged 17 commits into
masterfrom
1397-add-aria-labels-may-be-useful-for-testing
Dec 4, 2026
Merged

grzanka merged 17 commits into
masterfrom
1397-add-aria-labels-may-be-useful-for-testing

Conversation

@cnuart

@cnuart cnuart commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1397

This PR separates E2E test selectors from accessibility attributes to prevent changes to the accessibility layer or DOM structure from breaking Selenium tests.

Previously, Selenium tests relied on ARIA attributes and DOM structure to locate elements. As a result, accessibility improvements or markup changes could unintentionally break E2E tests.

With this change, data-testid is used exclusively for test automation, while ARIA attributes and HTML semantics are dedicated to accessibility. This establishes a clear separation of concerns and makes both the test suite and accessibility implementation more maintainable.

Key Principles

  • data-testid for testing, ARIA for accessibility. ARIA attributes must not be introduced solely to support tests, and data-testid should not be added indiscriminately to every element.
  • Prefer accessible names derived from visible content. Use visible text, <label>, string-based tooltips, or aria-labelledby whenever possible.
  • Use aria-label only when no visible text is available, for example, icon-only buttons, vector axes, or inputs that rely exclusively on placeholders.
  • Avoid redundant or conflicting accessible names. An aria-label must neither contradict nor unnecessarily duplicate visible text.
  • Follow MUI-specific accessibility requirements. For Checkbox and Switch, aria-label and aria-labelledby must be applied through slotProps.input, since applying them directly to the component places them on the root <span> rather than the actual input. For Modal, which uses role="presentation", the accessible dialog name must be assigned to the content container.
  • Keep test IDs consistent and unique. All data-testid values follow kebab-case naming, avoid file names and UI text, and must be unique across the entire DOM. This is particularly important for TabPanel components, which may coexist in the DOM even when not visible.

Test Infrastructure

Two new utilities have been introduced:

  • src/util/testIds.ts – A centralized registry of data-testid values, including constants and functions for dynamically generated IDs. Keys are based on domain concepts rather than component or file names.
  • src/test/seleniumUtils.ts – Introduces the byTestId(id) helper for locating elements by their test IDs.

Five Selenium tests have been migrated from ARIA-based and XPath selectors to byTestId.

TopasConverter.test.ts has been temporarily disabled using xtest. A comment documents that its selectors are outdated. No selector migration has been included for this test.

Accessibility Improvements

Automatically Generated IDs and Accessible Names

CustomDialog

  • Replaced title-based IDs with IDs generated by React's useId, preventing potential duplicate IDs.
  • Connected the dialog's title and description using aria-labelledby and aria-describedby.

PropertyField (Properties Panel)

  • Added automatic label ID generation using useId.
  • Exposed the generated label ID through the usePropertyFieldLabelId() context hook.
  • Fields now derive their accessible names from visible labels using aria-labelledby, without changing the DOM structure or styling.
  • Applied consistently to text, number, color, checkbox, select, autocomplete, and toggle group fields.
  • When a field has no associated label, no aria-labelledby attribute is generated, preventing dangling ARIA references.

Additional wrappers introduced in PropertyField.tsx:

  • LabelledSelect
  • LabelledAutoCompleteSelect
  • LabelledExclusiveToggleButtonGroup

AutoCompleteSelect now supports optional labelId and ariaLabel props. MaterialSelect and ParticleSelect retrieve the label ID directly from the context.

Dynamic Accessible Names

Accessible names are now composed from relevant application data where appropriate:

  • Visibility checkboxes in the object tree: Visibility: <object name>
  • Vector axes: <label> X/Y/Z
  • Conditional fields: <label> value
  • Boolean operation groups: Boolean operation: <geometry>

BatchParametersEditor

  • Added aria-labelledby to batch checkboxes, referencing their visible labels through IDs generated with useId.

Additional ARIA Improvements

  • Removed placeholder accessible names copied from MUI documentation.
  • Removed dangling aria-labelledby and aria-controls references.
  • Changed the left sidebar to semantic <nav> markup with the accessible name Main navigation.
  • Replaced aria-selected with aria-current="page" for the active navigation item.
  • Added role="dialog" and an accessible name to the Input files editor dialog.
  • Added accessible names to icon-only buttons.
  • Removed redundant aria-label attributes that duplicated visible text or tooltip content.

Expected Outcome

  • Selenium tests are independent of accessibility attributes and DOM implementation details.
  • Accessibility improvements can be introduced without requiring changes to E2E selectors.
  • Interactive elements have more consistent and meaningful accessible names.
  • ARIA relationships are correctly established, avoiding duplicate IDs and dangling references.
  • Test selectors are centralized, predictable, and easier to maintain.

Overall, this change establishes a clear boundary between test automation and accessibility, improving the long-term maintainability and reliability of both.

@cnuart cnuart linked an issue Nov 1, 2026 that may be closed by this pull request
@cnuart cnuart self-assigned this Nov 15, 2026
@cnuart
cnuart marked this pull request as ready for review November 15, 2026 13:36

@grzanka grzanka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good direction: a central test-id registry and real accessible names are a big step up from XPath on aria-label.

The main issue is that the Selenium migration weakened the tests. Several "tab became active" checks are now "element exists" checks that always pass, so navigation could break without any test failing. Please fix those (inline on SimulationControls.test.ts and FlukaConverter.test.ts) before merge. The prop-merging comments are small fixes. The rest can be follow-ups.

Prepared by Claude Opus 5.5 (Claude Code) at the request of @grzanka.

Comment thread src/__tests__/FlukaConverter.test.ts Resolved
Comment thread src/util/testIds.ts Resolved
@grzanka

grzanka commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

@cnuart please resolve merge conflicts and update PR description

@grzanka grzanka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, separating test IDs from ARIA is a good direction, and the useId-based dialog IDs fix real duplicate-ID problems. Two things need fixing before merging, plus a few smaller points inline.

1. The converter submodule goes backwards. src/libs/converter moves from 47dfd98 (master) to ea570da, which is older: it lacks "Add simulator detection for input files (#272)". PythonWorker.ts:108 still runs from converter.api import detect_simulators, which doesn't exist at ea570da, so simulator detection in the Input files panel breaks. This PR doesn't need a converter change, so git checkout origin/master -- src/libs/converter should be enough.

2. CI is red. ObjectInfo.test.ts:60 still clicks //div[@aria-label = 'Editor']. That aria-label is removed from NavPanelElement here, and the CI log shows the test timing out on that step in both attempts. Switching it to byTestId(testIds.navItem('editor')) like the other suites should fix it.

Smaller one, outside the diff: EditorSidebar.test.ts still opens the Add menu with By.id('menu-button-21') / menu-content-21, which depend on the menu's position index. Passing name to that MenuPosition and using testIds.menuButton(...) / testIds.menuPopup(...) would finish the migration for this suite.

Comment thread src/util/testIds.ts Resolved
@grzanka
grzanka added this pull request to the merge queue Dec 4, 2026
Merged via the queue into master with commit f5c3759 Dec 4, 2026
10 checks passed
@grzanka
grzanka deleted the 1397-add-aria-labels-may-be-useful-for-testing branch December 4, 2026 16:33
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.

add aria labels, may be useful for testing

2 participants