Test Example in Python

Link to Test Example in Python 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 Python

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

settings = {}
settings['apiKey'] = "<your-api-key-here>"
result = self.driver.execute_script('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.

settings = {}
settings['apiKey'] = "<your-api-key-here>"
pageSource = self.driver.page_source
result = self.driver.execute_script('axe:scan', settings, pageSource)

Full Example with UIAutomator2

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.appium_connection import AppiumConnection
import json

class AppiumPluginTest:

    def setup(self):
        success = True
        desired_caps = {}

        desired_caps['platformName'] = 'Android'
        desired_caps['automationName'] = 'UiAutomator2'
        desired_caps['appPackage'] = "com.android.settings"
        desired_caps['appActivity'] = ".Settings"

        options = UiAutomator2Options().load_capabilities(desired_caps)
        self.driver = webdriver.Remote('http://localhost:4723', options=options)

    def runAccessibilityScan(self):
        settings = {}
        settings['apiKey'] = "<your-api-key-here>"
        return self.driver.execute_script('axe:scan', settings)


demo = AppiumPluginTest()
demo.setup()

result = demo.runAccessibilityScan()

Full Example with XCUITest

from appium import webdriver
from appium.options.ios import XCUITestOptions
from appium.webdriver.appium_connection import AppiumConnection
import json

class AppiumPluginTest:

    def setup(self):
        success = True
        desired_caps = {}

        desired_caps['platformName'] = 'iOS'
        desired_caps['automationName'] = 'XCUITest'
        desired_caps['bundleId'] = 'com.dequesystems.axe-devtools-ios-sample-app'
        desired_caps['udid'] = '...' # xcrun simctl list | grep Booted

        options = XCUITestOptions().load_capabilities(desired_caps)
        self.driver = webdriver.Remote('http://localhost:4723', options=options)

    def runAccessibilityScan(self):
        settings = {}
        settings['apiKey'] = "<your-api-key-here>"
        return self.driver.execute_script('axe:scan', settings)


demo = AppiumPluginTest()
demo.setup()

result = demo.runAccessibilityScan()