Setup for Layout Agnostic Testing
For XML, Compose, or React Native apps.
This page highlights the steps for setting up the library to scan any layout (XML, Compose, or React Native) app for accessibility issues. For steps to pull in the library, please see Getting Started.
Other resources for scanning Android layout hierarchies:
- Setup for XML Layout Testing
- Setup for Compose Layout Testing
Automated Testing
Initialize the Library
In the test class init, connect to the library with one of the following:
Connect with API key:
Generate an API key at axe.deque.com.
private val axe = AxeDevTools()
init {
...
axe.connect("DEQUE_API_KEY")
...
}
Connect with username and password:
private val axe = AxeDevTools()
init {
...
axe.connect(
"DEQUE_USERNAME",
"DEQUE_PASSWORD"
)
...
}
Set the Instrumentation Registry
This is the piece that allows axe DevTools for Android to connect to the view hierarchy. This can be set before your tests run in the @Before
fun setup()
block.
private val axe = AxeDevTools()
@Before
fun setup() {
axe.setInstrumentation(InstrumentationRegistry.getInstrumentation())
}
Run a Scan
When you're ready to run a scan from within your tests, call axe.scan()
. We've included a helper function below that highlights some additional functionality you may find helpful.
Read about these features and more on the Android Features page. This function is broken down below the snippet.
private fun a11yScan() {
val scanResultHandler = axe.scan()
//1. Upload it to the dashboard
scanResultHandler?.uploadToDashboard()
//2. Using the results in your test suite
val result: AxeResult? = scanResultHandler?.getSerializedResult()
result?.axeRuleResults?.forEach { result ->
if(result.status == AxeStatus.PASS) {
}
else if(result.status == AxeStatus.FAIL) {
}
else if(result.status == AxeStatus.INCOMPLETE) {
}
}
//3. Save the result JSON to a local file for later use
scanResultHandler?.saveResultToLocalStorage("your_file_prefix")
}
Code snippet breakdown:
- From the result handler, call
uploadToDashboard()
to send the latest scan to the dashboard. The scan will be published from the authenticated account used in theinit
function. - From the result handler, call
getSerializedResult()
to access the library's results locally. For example, you may choose to fail the test when failures are found in the accessibility scan results. This will not upload your results to the server. Uploading your results is not required to download them locally. - From the result handler, call
saveResultToLocalStorage()
to save the results as JSON in a local file on your test device. To access the saved file, refer to the feature documentation for Saving Results Locally.
Deinitialize
Once the test has been completed, call tearDown()
on the AxeDevTools object you have created to clear the state of previous tests. We recommend putting this in your testing file's tearDown
function, but it can also be called within a test if needed.
@After
fun tearDown() {
axe.tearDown()
}
Example Espresso Test Class
class ExampleTest {
private val axe = AxeDevTools()
init {
// Connect using an API key
axe.connect(
"DEQUE_API_KEY"
)
}
@Before
fun setup() {
// Pass the information registry to axe DevTools
axe.setInstrumentation(InstrumentationRegistry.getInstrumentation())
// Optional: Add tags or utilize other customization features here
axe.tagScanAs(setOf("Team A"))
}
@Test
fun foobar() {
// Scan the app for accessibility issues and upload to the dashboard
axe.scan()?.uploadToDashboard()
axe.tearDown()
}
}