Setup for Compose Layouts

Link to Setup for Compose Layouts copied to clipboard

This page highlights the steps for setting up the library to scan Compose layouts for accessibility issues. For steps to pull in the library, please see Getting Started. For scanning XML layouts, please see Setup for XML Layouts.

important

Scanning Compose layouts for accessibility issues supported only in Espresso UI 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 = AxeDevToolsCompose()

init {
     axe.connect(
        "APIKEY",
        ConnectionConfig.DEFAULT_CONNECTION_CONFIG
    )
}

Connect with username and password:

private val axe = AxeDevToolsCompose()

init {
    axe.connect(
        "username",
        "password",
        ConnectionConfig.DEFAULT_CONNECTION_CONFIG
    )
}

Connect to the desktop application:

To get started with the desktop application, be sure to reference Desktop Setup.

private val axe = AxeDevToolsCompose()

init {
     axe.connectLocally("48485")
}

Run a Scan

When you're ready to run a scan from your tests, you'll want to run 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() {
    /*
    Note: This test relies on the compose test rule from `setup` method. Please see the full example at the bottom of this guide.
    */

    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:

  1. From the result handler, call uploadToDashboard() to send the most recent scan to the dashboard. The scan will be published from the authenticated account used in the init function.
  2. From the result handler, call getSerializedResult() to use results from the library 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 get the results locally.
  3. From the result handler, call saveResultToLocalStorage() to save the results as JSON in a local file within 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 AxeDevToolsCompose 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 for Compose


@ExperimentalComposeUiApi
class ExampleComposeTest {
    @get:Rule
    val composeTestRule = createAndroidComposeRule<TestComposeActivity>()

    private val axe = AxeDevToolsCompose()

    init {
        // Connect using an API key 
        axe.connect(
            "APIKey",
            ConnectionConfig.DEFAULT_CONNECTION_CONFIG
        )
    }

    @Before
    fun setup() {
        composeTestRule.setContent { YourComposableFunction() }
        composeTestRule.mainClock.autoAdvance = false
        composeTestRule.waitForIdle()

        axe.tagScanAs(setOf("ScanningAnApp"))
    }

    @Test
    fun foobar() {
        // Must be set in the tests itself
        axe.setComposeTestRule(composeTestRule)
        val scan = axe.scan()
        scan?.uploadToDashboard()
        axe.tearDown()
    }
}