Espresso

This page is not available in the language you requested. You have been redirected to the English version of the page.
Link to this page copied to clipboard
Not for use with personal data

Di seguito è riportato un esempio di come iniziare i test con axe DevTools con Espresso. Assicurati di consultare la sezione Funzionalità Android SDK per scoprire tutto ciò che puoi personalizzare nei tuoi test di accessibilità con axe DevTools Mobile.

Per i layout XML

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()
        }
    }
}

Per i layout di composizione

@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()
    }
}