Test Example in Java

Link to Test Example in Java copied to clipboard

Be sure to checkout the full Appium setup guide with axe DevTools Mobile if you're just getting started, or more examples of axe DevTools Mobile for Appium in other languages.

executeScript in Java

Initiate an accessibility scan by calling the following in your Java Appium tests:

Map<String, String> settings = ImmutableMap.of("apiKey", "<your-api-key-here>");
driver.executeScript("axe:scan", settings);

Example With Page Source

When Appium's page source API is incorporated into your tests, consider optimizing performance by passing it through the execute script method. While providing the page source is optional, we advise against it unless you are confident that the page source accurately reflects your application's current state and remains unmodified. For more precise results, it's recommended to refrain from passing the page source.

Map<String, String> settings = ImmutableMap.of("apiKey", "<your-api-key-here>");
String pageSource = driver.getPageSource();
driver.executeScript("axe:scan", settings, pageSource);

Full Example with UIAutomator2

import com.google.common.collect.ImmutableMap;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

public class AppiumPluginTest {
    AndroidDriver driver;

    AndroidDriver makeDriver() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        capabilities.setCapability("appium:appPackage", "com.android.settings");
        capabilities.setCapability("appium:appActivity", ".Settings");

        String DEFAULT_APPIUM_ADDRESS = "http://0.0.0.0:4723/";
        return new AndroidDriver(new URL(DEFAULT_APPIUM_ADDRESS), capabilities);
    }

    @Before
    public void setup() throws MalformedURLException {
        driver = makeDriver();
    }

    @Test
    public void test() {
        Map<String, String> settings = ImmutableMap.of("apiKey", "<your-api-key-here>");
        driver.executeScript("axe:scan", settings);
    }
}

Full Example with XCUITest

import com.google.common.collect.ImmutableMap;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;


public class AppiumPluginTest {
    IOSDriver driver;

    IOSDriver makeDriver() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.IOS);
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.IOS_XCUI_TEST);
        capabilities.setCapability("appium:bundleId", "com.dequesystems.axe-devtools-ios-sample-app");
        capabilities.setCapability("appium:udid", "..."); // xcrun simctl list | grep Booted

        String DEFAULT_APPIUM_ADDRESS = "http://0.0.0.0:4723/";
        return new IOSDriver(new URL(DEFAULT_APPIUM_ADDRESS), capabilities);
    }

    @Before
    public void setup() throws MalformedURLException {
        driver = makeDriver();
    }

    @Test
    public void test() {
        Map<String, String> settings = ImmutableMap.of("apiKey", "<your-api-key-here>");
        driver.executeScript("axe:scan", settings);
    }
}