This article explains how to handle app permissions during mobile test automation.
While testing different scenarios, its common for iOS or Android apps to show various pop-ups or system dialogs that prompt the user to grant various permissions (e.g. contacts, notifications, photos etc) to the app. There are various techniques to handle these interactions during automated test execution.
To test Android apps, use Appium’s autoGrantPermissions
capability to automatically determine which permissions your app requires and grant them to the app on install. These permissions are determined based on the Android manifest defined in the the app’s .APK
file. This cabability works on Appium 1.9.1 and above.
The sample below shows how to use autoGrantPermissions
capability for your Android tests:
caps.setCapability("autoGrantPermissions", "true");
let capabilities = {
'autoGrantPermissions': 'true'
}
capability.AddAdditionalCapability("autoGrantPermissions", "true");
desired_cap = {
'autoGrantPermissions' = 'true',
}
caps['autoAcceptAlerts'] = 'true'
noReset
cabability is set to true, autoGrantPermissions
capability doesn’t work.
To test iOS apps, use Appium’s autoAcceptAlerts
and autoDismissAlerts
capabilities to handle app permissions. autoAcceptAlerts
will automatically accept all permission pop-ups. autoDismissAlerts
will automatically dismiss all permission pop-ups. This includes privacy access permission pop-ups (e.g., location, contacts, photos).
The sample below shows how to use autoAcceptAlerts
or autoDissmissAlerts
capabilities for your test:
caps.setCapability("autoAcceptAlerts", "true"); //to accept all alerts
//OR
caps.setCapability("autoDissmissAlerts", "true"); //to dismiss all alerts
let capabilities = {
'autoAcceptAlerts': 'true' //to accept all alerts
}
//OR
let capabilities = {
'autoDissmissAlerts': 'true' //to dismiss all alerts
}
capability.AddAdditionalCapability("autoAcceptAlerts", "true"); //to accept all alerts
//OR
capability.AddAdditionalCapability("autoDissmissAlerts", "true"); //to accept all alerts
desired_cap = {
'autoAcceptAlerts': 'true', #to accept all alerts
}
#OR
desired_cap = {
'autoDissmissAlerts': 'true' #to dismiss all alerts
}
caps['autoAcceptAlerts'] = 'true' #to accept all alerts
#OR
caps['autoDissmissAlerts'] = 'true' #to accept all alerts
autoDismissAlerts
and autoAcceptAlerts
behaviour is flipped. For such popups, use autoAcceptAlerts
to automatically dismiss all popups and autoDismissAlerts
to automatically accept all popups.
For more selective test scenarios, you can accept some app permission pop-ups and deny the others. To automate these interactions,you can accept or deny individual pop-ups by finding their respective element locators on iOS and Android devices.
//For Android
driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
//For iOS
driver.findElement(By.id("Allow")).click();
//For Android
let element = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
await element.click();
//For iOS
let element = await driver.element("id", "Allow")
await element.click();
//For Android
IWebElement ll = driver.FindElement(By.XPath(".//android.widget.Button[@text='Allow']"));
ll.Click();
//For iOS
IWebElement ll = driver.FindElement(By.Id("Allow"));
ll.Click();
#For Android
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
#For iOS
driver.find_element_by_id("Allow").click()
#For Android
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click;
#For iOS
driver.find_element(:id, "Allow").click;
Contact our Support team for immediate help while we work on improving our docs.
Contact our Support team for immediate help while we work on improving our docs.
Thank you for your valuable feedback!