Features

Handling permission pop-ups

While testing your mobile app, it may ask for specific permissions during the test. Various system dialogs or popups may appear, asking the user to grant access.
The mobile app might ask the user permission to access the contacts, notifications, media, ... on the device.

Below we'll go over the various techniques to handle these events during your mobile testing:

Allow or Deny all permissions

Appium allows you to automatically accept all permission and alert requests.

Allow or Deny all permissions on Android

You can use the autoGrantPermissions capability.

Appium will inspect the Android Manifest of the app, extract the required permissions and grant these.

Copy code
caps['autoGrantPermissions'] = 'true'
Copy code
caps.setCapability("autoGrantPermissions", "true");
Copy code
desired_cap = {
  'autoGrantPermissions' = 'true',
}
Copy code
var capabilities = {
    'autoGrantPermissions': 'true'
}
Copy code
capability.AddAdditionalCapability("autoGrantPermissions", "true");

Allow or Deny all permissions on iOS

On iOS, Appium offers two capabilities which you can use: autoAcceptAlerts and autoDismissAlerts.

autoAcceptAlerts will automatically accept (grant) any alerts (including permission popups) that might appear during your test.
autoDismissAlerts will automatically dismiss (deny) any alerts (including permission popups) that might appear during your test.

Copy code
caps['autoAcceptAlerts'] = 'true' # to accept all alerts
caps['autoDissmissAlerts'] = 'true' # to accept all alerts
Copy code
caps.setCapability("autoAcceptAlerts", "true"); // to accept all alerts
caps.setCapability("autoDissmissAlerts", "true"); // to dismiss all alerts
Copy code
desired_cap = {
  'autoAcceptAlerts': 'true', # to accept all alerts
}
desired_cap = {
  'autoDissmissAlerts': 'true' # to dismiss all alerts
}
Copy code
var capabilities = {
    'autoAcceptAlerts': 'true' // to accept all alerts
}
var capabilities = {
    'autoDissmissAlerts': 'true' // to dismiss all alerts
}
Copy code
capability.AddAdditionalCapability("autoAcceptAlerts", "true"); // to accept all alerts
capability.AddAdditionalCapability("autoDissmissAlerts", "true"); // to accept all alerts

Customizing the alert button selector

There may be dialogs that appear during your automated tests which have 3 buttons instead of 2, or there's a specific button you want to press instead of the default (allow) button.

In this case, you might want to consider using the acceptAlertButtonSelector Appium setting, which allows you to pass a selector (a class-chain expression) to tell Appium which button to click.

For example, you might want to click the 'Allow Once' button in a location permission dialog. To instruct Appium to do this, you can change the acceptAlertButtonSelector setting during your test:

Copy code
driver.updateSettings({
    acceptAlertButtonSelector: "**/XCUIElementTypeButton[`label CONTAINS[c] 'Allow Once'`]"
});

Alternatively, you can set this setting at the beginning of your test with a capability:

Copy code
const capabilities = {
  'appium:settings[acceptAlertButtonSelector]' = "**/XCUIElementTypeButton[`label CONTAINS[c] 'Allow Once'`]"
}

Allow or Deny specific permissions

You can also choose to accept or deny alerts and popups during your test.
Your test will need to find the element of the alert or popup and perform a click action on that element.

Copy code
# For Android
driver.find_element(:xpath, ".//android.widget.Button[@text='Allow']").click
# For iOS
driver.find_element(:id, "Allow").click
Copy code
// For Android
driver.findElement(By.xpath(".//android.widget.Button[@text='Allow']")).click();
// For iOS
driver.findElement(By.id("Allow")).click();
Copy code
# For Android
driver.find_element_by_xpath(".//android.widget.Button[@text='Allow']").click()
# For iOS
driver.find_element_by_id("Allow").click()
Copy code
// For Android
var element = await driver.element("xpath", ".//android.widget.Button[@text='Allow']")
await element.click()
// For iOS
var element = await driver.element("id", "Allow")
await element.click()
Copy code
// 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();