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.
If you are using BrowserStack SDK, you can set the following capability within the browserstack.yml file:
Grant all permissions for Android apps
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:
The autoGrantPermissions capability doesn’t work if noReset cabability is set to true.
Allow or deny all permissions for iOS apps
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 autoDismissAlerts capabilities for your test:
For iOS 13 & above, pop-ups can have more than two buttons. When there are more than two buttons on popups, autoDismissAlerts and autoAcceptAlerts behaviour is flipped. For such popups, use autoAcceptAlerts to automatically dismiss all popups and autoDismissAlerts to automatically accept all popups.
BrowserStack SDK is a plug-n-play solution that takes care of all the integration steps for you. Using the BrowserStack SDK is the recommended integration method for your project. To know more, visit the SDK core concepts page.
Grant all permissions for Android apps
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:
The autoGrantPermissions capability doesn’t work if noReset cabability is set to true.
Allow or deny all permissions for iOS apps
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 autoDismissAlerts capabilities for your test:
DesiredCapabilities capabilities =newDesiredCapabilities();HashMap<String,Object> browserstackOptions =newHashMap<String,Object>();
browserstackOptions.put("autoAcceptAlerts","true");//to accept all alerts//ORDesiredCapabilities capabilities =newDesiredCapabilities();HashMap<String,Object> browserstackOptions =newHashMap<String,Object>();
browserstackOptions.put("autoDismissAlerts","true");//to dismiss all alerts
var capabilities ={'bstack:options':{"autoAcceptAlerts":"true"},}//to accept all alerts//ORvar capabilities ={'bstack:options':{"autoDismissAlerts":"true"},}//to dismiss all alerts
AppiumOptions capabilities =newAppiumOptions();Dictionary<string,object> browserstackOptions =newDictionary<string,object>();
browserstackOptions.Add("autoAcceptAlerts","true");//to accept all alerts//ORAppiumOptions capabilities =newAppiumOptions();Dictionary<string,object> browserstackOptions =newDictionary<string,object>();
browserstackOptions.Add("autoDismissAlerts","true");//to dismiss all alerts
desired_cap ={'bstack:options':{"autoAcceptAlerts"="true",},}#to accept all alerts#OR
desired_cap ={'bstack:options':{"autoDismissAlerts"="true",},}#to dismiss all alerts
capabilities ={'bstack:options'=>{"autoAcceptAlerts"=>"true"},}#to accept all alerts#OR
capabilities ={'bstack:options'=>{"autoDismissAlerts"=>"true"},}#to dismiss all alerts
caps.setCapability("autoAcceptAlerts","true");//to accept all alerts//OR
caps.setCapability("autoDismissAlerts","true");//to dismiss all alerts
let capabilities ={'autoAcceptAlerts':'true'//to accept all alerts}//ORlet capabilities ={'autoDismissAlerts':'true'//to dismiss all alerts}
capability.AddAdditionalCapability("autoAcceptAlerts","true");//to accept all alerts//OR
capability.AddAdditionalCapability("autoDismissAlerts","true");//to dismiss all alerts
desired_cap ={'autoAcceptAlerts':'true',#to accept all alerts}#OR
desired_cap ={'autoDismissAlerts':'true'#to dismiss all alerts}
caps['autoAcceptAlerts']='true'#to accept all alerts#OR
caps['autoDismissAlerts']='true'#to dismiss all alerts
For iOS 13 & above, pop-ups can have more than two buttons. When there are more than two buttons on popups, autoDismissAlerts and autoAcceptAlerts behaviour is flipped. For such popups, use autoAcceptAlerts to automatically dismiss all popups and autoDismissAlerts to automatically accept all popups.
Allow or Deny any permission pop-up for Android and iOS apps
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.