Espresso

Link to Espresso copied to clipboard
Free Trial

Below is an example of getting started with axe DevTools testing with Espresso. Be sure to checkout the Android SDK features section for everything you can customize in your accessibility tests with axe DevTools Mobile.

For XML Layouts

class ExampleInstrumentedTestWithAccessibility : Utils {

    @Rule
    @JvmField
    var rule: ActivityScenarioRule<MainActivity> = ActivityScenarioRule(MainActivity::class.java)

    private val axe = AxeDevTools()

    init {
        //Login using an API Key
        axe.loginWithApiKey(
            "<API_KEY>"
        )
    }

    @Before
    fun setup() {
        axe.tagScanAs(setOf("YourTag"))
        axe.setTestingConfig(AxeDevToolsEspressoConfig(IdlingRegistry.getInstance()))
    }

    @Test
    fun exampleTest() {
        onView(withText("Your View Name")).perform(click())
        onView(withText("Button")).perform(click())
        onView(withContentDescription("Content Description")).perform(click())
    }

    @After
    fun runAccessibilityScan() {
        rule.scenario.onActivity {
            val scan = axe.scan(it)
            scan?.uploadToDashboard()
            axe.tearDown()
        }
    }
}

For Compose Layouts

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

    private val axe = AxeDevToolsCompose()

    init {
        // Login using an API key 
        axe.loginWithApiKey(
            "<API_KEY>"
        )
    }

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

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

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