Add accessibility assertions in Automated tests
Add accessibility assertions to your automated test suite.
You can add accessibility assertions to your automated test suite using BrowserStack’s accessibility testing functions. These functions return either a summary of issues or a full list of detected issues, depending on your testing needs. This page explains how to use each method and includes example assertions to help you integrate accessibility testing into your build process.
Accessibility assertions can increase build times, as tests wait for scans to complete before returning the accessibility issue summary and details.
If you are running Espresso tests, create your test classes and add the BrowserStack assertions in the .java
files under the androidTest
directory. Then, build your test suite, upload it along with your app to BrowserStack, and run the test using API calls.
For detailed steps, see the Espresso documentation.
Assertions using summarized accessibility results
BrowserStack provides summary functions that return a high-level summary of the accessibility testing conducted within a test case. The functions are framework-specific:
- For Java and C#, use the
getResultsSummary()
function. - For Node.js and Python, use the
getAccessibilityResultsSummary()
function.
You can use these functions to create assertions based on the issue count. Add the function to your test cases to add assertions to your build.
Output fields
Fields returned by the summary function:
Key | Description | Type |
---|---|---|
totalIssueCount |
Summation of issue count | Integer |
issueCountBySeverity |
Issue breakdown by severity | {critical : Integer, minor : Integer, moderate : Integer, serious : Integer} |
Output example
Example assertion
Map<String, Object> summary = AccessibilityUtils.getResultsSummary(driver);
int criticalIssueCount = Integer.parseInt(String.valueOf(((Map)summary.get("issueCountBySeverity")).get("critical")));
Assert.assertTrue(criticalIssueCount < 10, "Critical issue count breached the threshold!");
...
console.log('calling getAccessibilityResultsSummary');
var summary = await browser.getAccessibilityResultsSummary();
console.log(summary);
let criticalIssueCount = summary?.issueCountBySeverity?.critical ?? 0;
assert.strictEqual(criticalIssueCount < 10, true, `Critical issue count exceeded threshold! Found: ${criticalIssueCount}`);
...
For Java-based automated tests, import com.browserstack.accessibility.AccessibilityUtils
in your test cases to access accessibility result functions.
Assertions using detailed accessibility results
BrowserStack provides issue-level functions that return detailed accessibility issues based on the framework you use:
- For Java and C#, use the
getResults()
function. - For Node.js and Python, use the
getAccessibilityResults()
function.
You can use these functions to create assertions based on the actual issues details. Add the function to your test cases to add assertions to your build.
Output fields
Fields returned by the issue-level function:
Key | Description | Type |
---|---|---|
Issue name |
Name of the accessibility issue. | String |
Description |
Detailed description of the issue. | String |
WCAG Info |
WCAG guideline which has been violated. | String |
severity |
The severity level of the issue as per WCAG guidelines. | String |
className |
The class associated with the element having the issue. | String |
viewId |
The identifier for the view where the issue was found. | String |
Output example
Example assertion
/* getColorContrastIssueCount() represents a function which checks if any color contrast issues are present within the results. */
ArrayList<Map<String, Object>> results = AccessibilityUtils.getResults(driver);
int colorContrastIssueCount = getColorContrastIssueCount(results);
Assert.assertTrue(colorContrastIssueCount < 100, "Color contrast issue count breached the threshold!");
...
let results = await browser.getAccessibilityResults();
console.log("Accessibility Results:", results);
if (!results) {
throw new Error("Accessibility results not available! Test cannot proceed.");
}
function getAccessibilityLabelIssueCount(results) {
return results.filter(issue => issue.issueName === "Interactive Element Accessibility Label").length;
}
let accessibilityLabelIssueCount = getAccessibilityLabelIssueCount(results);
console.log(`Interactive Element Accessibility Label Issues Found: ${accessibilityLabelIssueCount}`);
assert.strictEqual(accessibilityLabelIssueCount < 50, true, `Accessibility Label issue count exceeded threshold! Found: ${accessibilityLabelIssueCount}`);
...
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!