Test Example in C#

Link to Test Example in C# copied to clipboard
Free Trial

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 C#

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

axeSettings = new Dictionary<string, string>();
axeSettings.Add("apiKey", "<your-api-key>");
var result = _driver.ExecuteScript("axe:scan", axeSettings);

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.

axeSettings = new Dictionary<string, string>();
axeSettings.Add("apiKey", "<your-api-key>");
var pageSource = _driver.PageSource;
var result = _driver.ExecuteScript("axe:scan", axeSettings, pageSource);

Full Example with UIAutomator2

using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;

namespace appiumtest;

public class Tests
{
    private AndroidDriver _driver;
    private Dictionary<string, string> axeSettings;

    public void setApiKey() {
        axeSettings = new Dictionary<string, string>();
        axeSettings.Add("apiKey", "<your-api-key>");
    }
    
    [OneTimeSetUp]
    public void SetUp()
    {
        setApiKey();

        var serverUri = new Uri(Environment.GetEnvironmentVariable("APPIUM_HOST") ?? "http://localhost:4723/");
        var driverOptions = new AppiumOptions() {
            AutomationName = AutomationName.AndroidUIAutomator2,
            PlatformName = "Android",
        };

        driverOptions.AddAdditionalAppiumOption("appPackage", "com.android.settings");
        driverOptions.AddAdditionalAppiumOption("appActivity", ".Settings");
        driverOptions.AddAdditionalAppiumOption("noReset", true);

        _driver = new AndroidDriver(serverUri, driverOptions, TimeSpan.FromSeconds(180));
        _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
    }

    [OneTimeTearDown]
    public void TearDown()
    {
        _driver.Dispose();
    }

    [Test]
    public void Test()
    {   
        var result = _driver.ExecuteScript("axe:scan", axeSettings);
    }
}

Full Example with XCUITest

using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.iOS;

namespace appiumtest;

public class AppiumPluginTest
{
    private IOSDriver _driver;
    private Dictionary<string, string> axeSettings;

    public void setApiKey() {
        axeSettings = new Dictionary<string, string>();
        axeSettings.Add("apiKey", "<your-api-key>");
    }
    
    [OneTimeSetUp]
    public void SetUp()
    {
        setApiKey();

        var serverUri = new Uri(Environment.GetEnvironmentVariable("APPIUM_HOST") ?? "http://localhost:4723/");
        var driverOptions = new AppiumOptions() {
            AutomationName = "XCUITest",
            PlatformName = "iOS",
        };

        driverOptions.AddAdditionalAppiumOption("bundleId", "com.dequesystems.axe-devtools-ios-sample-app");
        driverOptions.AddAdditionalAppiumOption("udid", ""); // on Mac terminal: xcrun simctl list | grep Booted

        _driver = new IOSDriver(serverUri, driverOptions, TimeSpan.FromSeconds(180));
        _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
    }

    [OneTimeTearDown]
    public void TearDown()
    {
        _driver.Dispose();
    }

    [Test]
    public void Test()
    {   
        var result = _driver.ExecuteScript("axe:scan", axeSettings);
    }
}