Class AccessibilityHelper
AxeBuilder.
The class exposes static methods that run an axe scan against a live
WebDriver session, persist the results as JSON and an interactive HTML
report, and attach the report to the current Allure test run as an attachment.
Typical usage:
// Quick fire-and-forget scan with default configuration
AccessibilityHelper.analyzePageAccessibility(driver, "HomePage");
// Scan and obtain a structured result object for in-test assertions
AccessibilityResult result = AccessibilityHelper.analyzePageAccessibilityAndSave(
driver, "CheckoutPage", true);
System.out.println("Accessibility score: " + result.getAccessibilityScore() + "%");
Thread safety: DATE_FORMAT and
DISPLAY_DATE_FORMAT are immutable DateTimeFormatter instances, so formatting
operations are safe to call from parallel test threads. However, concurrent scans
of the same pageName within the same second may produce file-name collisions
in the shared reports directory; use distinct page names or unique report directories
when running parallel scans on the same page.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classMutable configuration bean for an accessibility scan with fluent-builder setters.static classValue object returned byanalyzePageAccessibilityAndSave(WebDriver, String, boolean)and its overloads, carrying the aggregated outcome of a single axe accessibility scan. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidanalyzePageAccessibility(org.openqa.selenium.WebDriver driver, String pageName) Runs a WCAG accessibility scan against the currently loaded page using the defaultAccessibilityHelper.AccessibilityConfigand writes both a JSON data file and an interactive HTML report to disk.static voidanalyzePageAccessibility(org.openqa.selenium.WebDriver driver, String pageName, AccessibilityHelper.AccessibilityConfig config) Runs a WCAG accessibility scan using the suppliedAccessibilityHelper.AccessibilityConfigand writes both a JSON data file and an interactive HTML report to disk.static voidanalyzePageAccessibility(org.openqa.selenium.WebDriver driver, String pageName, String context) Convenience overload that runs an accessibility scan scoped to a specific CSS context selector using default settings for all other configuration values.analyzePageAccessibilityAndSave(org.openqa.selenium.WebDriver driver, String pageName, boolean saveReport) Runs an accessibility scan with default configuration, optionally persisting reports to disk, and returns a structuredAccessibilityHelper.AccessibilityResult.analyzePageAccessibilityAndSave(org.openqa.selenium.WebDriver driver, String pageName, AccessibilityHelper.AccessibilityConfig config, boolean saveReport) Full-control overload: runs an accessibility scan with the suppliedAccessibilityHelper.AccessibilityConfig, optionally saves reports, and returns a structuredAccessibilityHelper.AccessibilityResultsuitable for programmatic assertions.static voidattachFilteredReportToAllure(String pageName, AccessibilityHelper.AccessibilityResult result, org.openqa.selenium.WebDriver driver) Generates a filtered HTML report containing only the violations present in the suppliedAccessibilityHelper.AccessibilityResultand attaches it to the current Allure test step.static voidattachReportToAllure(String pageName) Attaches the most recently generated HTML accessibility report for the given page to the current Allure test step.static voidgenerateFilteredHTMLReport(AccessibilityHelper.AccessibilityResult result, String pageName, String reportPath, org.openqa.selenium.WebDriver driver) Generates a minimal HTML accessibility report that lists only the violations contained in the suppliedAccessibilityHelper.AccessibilityResultand writes it to the given file path.static PathgetLatestReportPath(String pageName) Finds the most recently modified HTML report file for the given page inside the"accessibility-reports/"directory.getViolationsByType(org.openqa.selenium.WebDriver driver) Returns a breakdown of axe violations grouped by their WCAG conformance level (e.g.static booleanhasCriticalViolations(org.openqa.selenium.WebDriver driver, String pageName) Determines whether the currently loaded page has any axe-detected WCAG violations using the default rule set.static booleanisAccessible(org.openqa.selenium.WebDriver driver) Performs a quick axe accessibility check and returns whether the page has zero violations.
-
Method Details
-
analyzePageAccessibility
Runs a WCAG accessibility scan against the currently loaded page using the defaultAccessibilityHelper.AccessibilityConfigand writes both a JSON data file and an interactive HTML report to disk. The HTML report is also attached to the current Allure test step automatically.AccessibilityHelper.analyzePageAccessibility(driver, "HomePage");- Parameters:
driver- activeWebDriverinstance pointing at the page to scan; must not benullpageName- human-readable label used in the report file name and Allure attachment title; must not be blank
-
analyzePageAccessibility
public static void analyzePageAccessibility(org.openqa.selenium.WebDriver driver, String pageName, AccessibilityHelper.AccessibilityConfig config) Runs a WCAG accessibility scan using the suppliedAccessibilityHelper.AccessibilityConfigand writes both a JSON data file and an interactive HTML report to disk. The HTML report is also attached to the current Allure test step automatically.The method blocks until
document.readyState === "complete"and the DOM has been stable for 1500 ms before invoking axe.AccessibilityConfig config = new AccessibilityConfig() .setTags(List.of("wcag21aa")) .setReportsDir("target/a11y/"); AccessibilityHelper.analyzePageAccessibility(driver, "CheckoutPage", config);- Parameters:
driver- activeWebDriverinstance pointing at the page to scan; must not benullpageName- human-readable label used in the report file name and Allure attachment title; must not be blankconfig- scan configuration; must not benull- Throws:
IllegalArgumentException- ifdriver,pageName, orconfigfails validation
-
analyzePageAccessibility
public static void analyzePageAccessibility(org.openqa.selenium.WebDriver driver, String pageName, String context) Convenience overload that runs an accessibility scan scoped to a specific CSS context selector using default settings for all other configuration values.AccessibilityHelper.analyzePageAccessibility(driver, "Checkout", "#checkout-form");- Parameters:
driver- activeWebDriverinstance; must not benullpageName- label used for the report file name and Allure attachment titlecontext- CSS selector restricting the scan scope (e.g."main, footer")
-
hasCriticalViolations
Determines whether the currently loaded page has any axe-detected WCAG violations using the default rule set.if (AccessibilityHelper.hasCriticalViolations(driver, "SearchResults")) { throw new AssertionError("Accessibility violations detected"); }- Parameters:
driver- activeWebDriverinstance; must not benullpageName- label used for log and error messages- Returns:
trueif at least one axe violation was found;falseotherwise- Throws:
RuntimeException- if the axe analysis itself fails
-
getViolationsByType
Returns a breakdown of axe violations grouped by their WCAG conformance level (e.g."WCAG 2.1 AA","Best Practice").The map value represents the total number of affected elements (nodes), not the number of distinct rules.
Map<String, Integer> counts = AccessibilityHelper.getViolationsByType(driver); counts.forEach((type, count) -> System.out.printf("%s: %d affected element(s)%n", type, count));- Parameters:
driver- activeWebDriverinstance; must not benull- Returns:
- map of WCAG level label to affected-element count; never
null - Throws:
RuntimeException- if the axe analysis fails
-
isAccessible
public static boolean isAccessible(org.openqa.selenium.WebDriver driver) Performs a quick axe accessibility check and returns whether the page has zero violations.assertTrue(AccessibilityHelper.isAccessible(driver), "Page should have no accessibility violations");- Parameters:
driver- activeWebDriverinstance; must not benull- Returns:
trueif no axe violations were found;falseif at least one violation exists or if the analysis could not be completed
-
analyzePageAccessibilityAndSave
public static AccessibilityHelper.AccessibilityResult analyzePageAccessibilityAndSave(org.openqa.selenium.WebDriver driver, String pageName, boolean saveReport) Runs an accessibility scan with default configuration, optionally persisting reports to disk, and returns a structuredAccessibilityHelper.AccessibilityResult.AccessibilityResult result = AccessibilityHelper.analyzePageAccessibilityAndSave(driver, "Checkout", true); assertFalse(result.hasViolations(), "No accessibility violations expected");- Parameters:
driver- activeWebDriverinstance; must not benullpageName- label for the report and Allure attachment; must not be blanksaveReport-trueto write JSON/HTML reports and attach them to Allure- Returns:
- an
AccessibilityHelper.AccessibilityResultcontaining violation details and score - Throws:
RuntimeException- if the axe analysis fails
-
analyzePageAccessibilityAndSave
public static AccessibilityHelper.AccessibilityResult analyzePageAccessibilityAndSave(org.openqa.selenium.WebDriver driver, String pageName, AccessibilityHelper.AccessibilityConfig config, boolean saveReport) Full-control overload: runs an accessibility scan with the suppliedAccessibilityHelper.AccessibilityConfig, optionally saves reports, and returns a structuredAccessibilityHelper.AccessibilityResultsuitable for programmatic assertions.The accessibility score is calculated as:
Incomplete checks are excluded from the score calculation.score = (passesCount / (passesCount + violationsCount)) * 100AccessibilityConfig config = new AccessibilityConfig() .setTags(List.of("wcag21aa")) .setContext("#content"); AccessibilityResult result = AccessibilityHelper.analyzePageAccessibilityAndSave(driver, "Home", config, true); System.out.printf("Score: %.1f%% (%d violation(s))%n", result.getAccessibilityScore(), result.getViolationsCount());- Parameters:
driver- activeWebDriverinstance; must not benullpageName- label for the report and Allure attachment; must not be blankconfig- scan configuration; a default instance is used whennullsaveReport-trueto write JSON/HTML reports and attach them to Allure- Returns:
- an
AccessibilityHelper.AccessibilityResultcontaining violation details and score - Throws:
IllegalArgumentException- if the supplied context selector matches no elements on the pageRuntimeException- if the axe analysis fails for any other reason
-
attachReportToAllure
Attaches the most recently generated HTML accessibility report for the given page to the current Allure test step.The method looks for the latest report file matching
AccessibilityReport_<pageName>*inside the default"accessibility-reports/"directory.AccessibilityHelper.attachReportToAllure("HomePage");- Parameters:
pageName- label used to locate the report file; must match the value passed when the report was originally generated
-
attachFilteredReportToAllure
public static void attachFilteredReportToAllure(String pageName, AccessibilityHelper.AccessibilityResult result, org.openqa.selenium.WebDriver driver) Generates a filtered HTML report containing only the violations present in the suppliedAccessibilityHelper.AccessibilityResultand attaches it to the current Allure test step.This is useful when a full report already exists but you want a focused attachment highlighting only the failing rules.
AccessibilityResult result = AccessibilityHelper.analyzePageAccessibilityAndSave(driver, "Cart", true); if (result.hasViolations()) { AccessibilityHelper.attachFilteredReportToAllure("Cart", result, driver); }- Parameters:
pageName- label used in the Allure attachment title and report file nameresult-AccessibilityHelper.AccessibilityResultwhose violations will be rendereddriver- activeWebDriverinstance used to read browser metadata for the report header
-
generateFilteredHTMLReport
public static void generateFilteredHTMLReport(AccessibilityHelper.AccessibilityResult result, String pageName, String reportPath, org.openqa.selenium.WebDriver driver) throws IOException, org.json.JSONException Generates a minimal HTML accessibility report that lists only the violations contained in the suppliedAccessibilityHelper.AccessibilityResultand writes it to the given file path.The output is suitable for attaching directly to an Allure report via
attachFilteredReportToAllure(String, AccessibilityResult, WebDriver).AccessibilityHelper.generateFilteredHTMLReport( result, "Checkout", "target/a11y/filtered.html", driver);- Parameters:
result-AccessibilityHelper.AccessibilityResultproviding the violation datapageName- page label rendered in the report headingreportPath- file-system path where the HTML file will be writtendriver- activeWebDriverused to read browser metadata for the report header- Throws:
IOException- if the report file cannot be created or writtenorg.json.JSONException- if serialising the violation data fails
-
getLatestReportPath
Finds the most recently modified HTML report file for the given page inside the"accessibility-reports/"directory.Path latestReport = AccessibilityHelper.getLatestReportPath("HomePage"); if (latestReport != null) { System.out.println("Latest report: " + latestReport); }- Parameters:
pageName- label used to filter report files by their name prefix- Returns:
- the
Pathof the newest matching report, ornullif the reports directory does not exist or contains no matching files - Throws:
IOException- if reading the directory listing fails
-