If missed the previous blog can check below: Web Page Accessibility and How to Test it manually https://www.tech-talks.info/post/web-page-accessibility-and-how-to-test-it-manually-and-through-test-automation
One can Automate the Web Accessibility using Deque Axe Library. To add it to your Selenium Web Driver test you need to follow the below step.
1. Add the dependency of Axe Library in POM.xml or Build .gradle.
Dependencies to be added in POM.xml
<dependency>
<groupId> com.deque.html.axe-core</groupId>
<artifactId>selenium</artifactId>
<version>4.1.2</version>
</dependency>
Dependencies to be added in build.gradle:
compile group: ‘com.deque.html.axe-core’, name: ‘selenium’, version: ‘4.1.2’,changing: true
2. Please refer https://github.com/dequelabs/axe-core-maven-html#readme to add the library to your test.The AxeBuilder type is the main interface. Pass it a Selenium WebDriver instance, configure it, and run the analysis method to get results.options wires a JSON string to axe, allowing rules to be toggled on or off. See the testAccessibilityWithOptions unit test for a sample single-rule execution, and the axe-core API documentation for full documentation on the options object. The run-only option with tags may be of particular interest, allowing the axe to execute all rules with the specified tag(s).include adds to the list of included selectors. If you do not call include at all, the axe will run against the entire document.exclude adds to the list of excluded selectors. Exclusions allow you to focus scope exactly where you need it, ignoring child elements you don’t want to test.with options takes an options object to be passed to the axe. run call.with tags, limits rules run to those that match specified tags.withOnlyRules limits rules run to those specified.disabled rules disables rules.analyze executes axe with any configuration you have previously defined. If you want to test one or more WebElements, you may pass them into analyze instead of using include and exclude.
3. For Sample Code below. Create a Feature File step to Verify Accessibility And Create Step Definition and In-Page Object Create CheckAccessibility Method for using in Verify Accessibility Step Definition. Provide AUT URL in Config/Environment Property File or where ever you pass your properties.
StepDefinition file:
Config/Env PropertyFile:
Runtime TestNG config:
PageObject:
In Axe Builder Pass the Driver instance. Please note I have taken a Driver instance in getDriver() method in BasePO Class.
package Hit.pageobjects;import automation.library.reporting.Reporter;
import automation.library.selenium.exec.BasePO;
import com.deque.axe.AXE;
import com.deque.html.axecore.results.Results;
import com.deque.html.axecore.results.Rule;
import com.deque.html.axecore.selenium.AxeBuilder;
import com.deque.html.axecore.selenium.AxeReporter;
import com.deque.html.axecore.selenium.ResultType;
import org.testng.Assert;import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;import static com.deque.html.axecore.selenium.AxeReporter.getAxeResultString;
import static com.deque.html.axecore.selenium.AxeReporter.getReadableAxeResults;public class MarknSpencerLandingPage extends BasePO {public void CheckAccessibility(){
Results results = new AxeBuilder().analyze(getDriver());
List<Rule> violations = results.getViolations();
if(violations.size()==0){
Reporter.addStepLog(“No Violation found”);
}
String AxeReportPath = System.getProperty(“user.dir”)+ File.separator + “AxeReports” + File.separator;
String timeStamp = new SimpleDateFormat(“yyyy_MM_dd_HH_mm_ss”).format(new java.util.Date());
String AxeViolationReportPath=AxeReportPath+ “AccessibilityViolations_ “ + timeStamp;AxeReporter.writeResultsToJsonFile(AxeViolationReportPath,results);
if(getReadableAxeResults(ResultType.Violations.getKey(),getDriver(),violations) ){
AxeReporter.writeResultsToTextFile(AxeViolationReportPath, AxeReporter.getAxeResultString());
}}}
Results:
Results Analysis: As One can see the hidden Element is focusable. As a result of which Screen reader would read the Hidden Element.
Conclusion:
Web Accessibility makes the life of people with disabilities easy and provides them equal opportunity. So the design and testing of Web Accessibility should be done thoroughly.
Comments