Test Example in JavaScript

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

Be sure to check out 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.

Full Example with UIAutomator2

const webdriverio = require('webdriverio')

describe('DebugTest', () => {
  let driver
  //The axeSettings object still accepts apiKey and axeServiceUrl but is not required if you are going to make one time beforeAll annotated call to 'mobile: axeStartSession'
  const axeSettings = {
    tags: ['appium', 'qa'],
    uploadToDashboard: true,
    ignoreRules: ['ScreenOrientation'],
  }
  const apiKey = '<YOUR_API_KEY>'
  const projectId = '<YOUR_DEV_HUB_PROJECT_ID>'

  //It is very important to make a call to 'mobile: axeStartSession' inside beforeAll annotated methods to setup your session for posting to axe Developer Hub.
  beforeAll(async () => {
    const capabilities = {
      platformName: 'Android',
      'appium:deviceName': 'Android',
      'appium:appPackage': '<YOUR_APP_PACKAGE_NAME>',
      'appium:appActivity': '.MainActivity',
      'appium:automationName': 'AxeUiAutomator2',
      'appium:uiautomator2ServerLaunchTimeout': 60000,
      'appium:uiautomator2ServerInstallTimeout': 60000,
      'appium:adbExecTimeout': 60000,
      'appium:ignoreHiddenApiPolicyError': true,
      'appium:disableWindowAnimation': true,
      'appium:waitForIdle': true,
      'appium:commandTimeout': 300,
      'appium:noReset': false,
      'appium:fullReset': false,
    }

    driver = await webdriverio.remote({
      protocol: 'http',
      hostname: 'localhost',
      port: 4723,
      path: '/',
      capabilities,
    })

    //Make one time call to setup your session for posting to Dev Hub.
    //This also accepts axeServiceUrl in case of private instance.
    const axeAuthSettings = {
      apiKey: apiKey,
      projectId: projectId,
    }
    await driver.execute('mobile: axeStartSession', axeAuthSettings)
  }, 60000)

  afterAll(async () => {
    if (driver) {
      await driver.deleteSession()
    }
  })

  const dismissSystemUIError = async () => {
    try {
      const systemPopup = await driver.$('//*[contains(@text, "System UI")]')
      await systemPopup.waitForDisplayed({ timeout: 5000 })

      if (await systemPopup.isDisplayed()) {
        const waitButton = await driver.$('//*[contains(@text, "Wait")]')
        await waitButton.waitForDisplayed({ timeout: 5000 })
        await waitButton.click()
        console.log("Dismissed the System UI error popup by clicking 'Wait'.")
      }
    } catch (e) {
      console.log('No System UI error popup appeared.')
    }
  }

  const launchApp = async () => {
    try {
      await driver.terminateApp('<YOUR_APP_PACKAGE_NAME>')
      await driver.pause(1000) // Give the app time to fully terminate
    } catch (e) {
      console.log(`App was not running or failed to terminate: ${e.message}`)
    }

    await driver.activateApp('<YOUR_APP_PACKAGE_NAME>')

    try {
      const element = await driver.$('//*[contains(@text, "Screen Name")]')
      await element.waitForDisplayed({ timeout: 30000 })
    } catch (e) {
      await dismissSystemUIError()
      // Try again after dismissing the error (if not, then fail)
      const element = await driver.$('//*[contains(@text, "Screen Name")]')
      await element.waitForDisplayed({ timeout: 30000 })
    }
  }

  beforeEach(async () => {
    await launchApp()
  })

  //Now since your session is authenticated you can keep making  'mobile: axeScan' call like below in test1 and test2. The scans will be uploaded to the dashboard and axe Developer Hub.
  it('test1', async () => {
    const element = await driver.$('//*[contains(@text, "Screen Name")]')
    await element.waitForDisplayed({ timeout: 10000 })
    console.log(await element.getText())

    const appiumScanResult = await driver.execute(
      'mobile: axeScan',
      axeSettings
    )
    const results = appiumScanResult.axeRuleResults
    console.log('debug: Total results: ' + results.length)
  })

  it('test2', async () => {
    const element = await driver.$('//*[contains(@text, "Screen Name")]')
    await element.waitForDisplayed({ timeout: 10000 })
    element.click()
    await driver.$('//*[contains(@text, "announced by a screen")]')

    const appiumScanResult = await driver.execute(
      'mobile: axeScan',
      axeSettings
    )
    const results = appiumScanResult.axeRuleResults
    console.log('debug: Total results: ' + results.length)
  })
})

Full Example with XCUITest

const { remote } = require('webdriverio')

describe('iOS Maps DebugTest', () => {
  let driver

  //The axeSettings object still accepts apiKey and axeServiceUrl but is not required if you are going to make one time beforeAll annotated call to 'mobile: axeStartSession'
  const axeSettings = {
    tags: ['appium', 'qa', 'ios'],
    uploadToDashboard: true,
    ignoreRules: ['ScreenOrientation'],
  }
  const apiKey = '<YOUR_API_KEY>'
  const projectId = '<YOUR_DEV_HUB_PROJECT_ID>'

  //It is very important to make a call to 'mobile: axeStartSession' inside beforeAll annotated method to setup your session for posting to axe Developer Hub.
  beforeAll(async () => {
    // Matching the working sample - minimal capabilities
    const wdOpts = {
      hostname: '127.0.0.1',
      port: 4723,
      logLevel: 'info',
      capabilities: {
        platformName: 'iOS',
        'appium:automationName': 'AxeXCUITest',
        'appium:udid': '<YOUR_DEVICE_OR_SIMULATOR_UDID>',
        'appium:bundleId': 'com.apple.Maps',
        'appium:wdaLaunchTimeout': 960000, // 16 minutes like the working sample
      },
    }

    driver = await remote(wdOpts)

    //Make one time call to setup your session for posting to Dev Hub.
    //This also accepts axeServiceUrl in case of private instance.
    const axeAuthSettings = {
      apiKey: apiKey,
      projectId: projectId,
      axeServiceUrl: '<YOUR_PRIVATE_INSTANCE_URL>', // Optional
    }
    await driver.execute('mobile: axeStartSession', axeAuthSettings)
  }, 1000000)

  afterAll(async () => {
    if (driver) {
      await driver.pause(1000)
      await driver.deleteSession()
    }
  })
  const launchApp = async () => {
    await driver.activateApp('com.apple.Maps')
  }

  beforeEach(async () => {
    await launchApp()
  })

  //Now since your session is authenticated you can keep making  'mobile: axeScan' call like below in test1 and test2. The scans will be uploaded to the dashboard and axe Developer Hub.
  it('test1 - scan main screen', async () => {
    const appiumScanResult = await driver.execute(
      'mobile: axeScan',
      axeSettings
    )
    const results = appiumScanResult.axeRuleResults
  }, 10000)

  it('test2 - click search and scan', async () => {
    const appiumScanResult = await driver.execute(
      'mobile: axeScan',
      axeSettings
    )
    const results = appiumScanResult.axeRuleResults
  }, 10000)
})