Exemplo de Teste em JavaScript
Not for use with personal data
Certifique-se de conferir o guia de configuração do Appium com Axe DevTools Mobile completo se você está começando agora, ou mais exemplos de Axe DevTools Mobile para Appium em outros idiomas.
Exemplo Completo com UIAutomator2
const webdriverio = require('webdriverio')
describe('DebugTest', () => {
let driver
//The axeSettings object still accepts apiKey and axeAccountURL 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 axeAccountURL in case of private instance.
const axeAuthSettings = {
apiKey: apiKey,
projectId: projectId,
}
await driver.execute('mobile: axeStartSession', axeAuthSettings)
}, 60000)
afterAll(async () => {
if (driver) {
// Generate an HTML report from all accumulated scans before ending the session.
// The report aggregates every 'mobile: axeScan' call made during this session.
try {
const reportSettings = {
scanName: 'My Accessibility Report',
axeHtmlReportPath: 'build/AxeDevToolsMobileResults',
}
const reportResult = await driver.execute(
'mobile: axeGenerateHtmlReportAndSummary',
reportSettings
)
if (reportResult.axeError) {
console.log('Report generation failed: ' + reportResult.axeError)
} else {
console.log('HTML report saved to: ' + reportResult.localDirectory)
}
} catch (e) {
console.log('Failed to generate HTML report: ' + e.message)
}
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)
})
})Exemplo Completo com XCUITest
const { remote } = require('webdriverio')
describe('iOS Maps DebugTest', () => {
let driver
//The axeSettings object still accepts apiKey and axeAccountURL 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 axeAccountURL in case of private instance.
const axeAuthSettings = {
apiKey: apiKey,
projectId: projectId,
axeAccountURL: '<YOUR_PRIVATE_INSTANCE_URL>', // Optional
}
await driver.execute('mobile: axeStartSession', axeAuthSettings)
}, 1000000)
afterAll(async () => {
if (driver) {
// Generate HTML reports before ending the session
await driver.execute('mobile: axeGenerateHtmlReportAndSummary', {})
// Alternative: Generate HTML reports to a custom path
// await driver.execute('mobile: axeGenerateHtmlReportAndSummary', {
// outputPath: '/Users/me/MyReports',
// })
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)
})