Handling alerts in an iOS web app using Appium

I’m working on automating a web app on iOS using Java and TestNG in combination with LambdaTest. My current challenge involves handling a location permission pop-up on Safari during geolocation testing. Despite switching to the NATIVE_APP context and using different locator strategies, I’m unable to automate clicking the Allow Once button.

Here’s a brief summary of my setup:

  • Device: iPhone 15 Pro, iOS 17.0
  • Tool: Appium with LambdaTest
  • Error: Unable to identify or click the “Allow Once” button on the pop-up.

:bulb: If anyone has successfully handled iOS system pop-ups or has insights into solving this issue, I would greatly appreciate your advice!

Hey Saanvi,

You need to set the capability autoAcceptAlerts to true.

For additional help, you can refer to the repository Appium iOS Popup Demo, which should assist with resolving the location pop-up issue in Safari.

We also used The Internet as the test URL.

Hope this helps! :blush:

Apart from the capability I have mentioned above, some more things can be done by you.

Here is what you need to do:

Set autoAcceptAlerts to true. This capability will automatically accept any alerts from the app. However, the alert that the user is getting is from the native app (i.e., Safari browser). This pop-up needs to be handled with a different logic, which is described in the next point.

This is what his capabilities should look like:

capabilities.setCapability("deviceName", "iPhone.*"); capabilities.setCapability("platformVersion", "15"); capabilities.setCapability("platformName", "ios"); capabilities.setCapability("isRealMobile", true); capabilities.setCapability("video", true); capabilities.setCapability("visual", true); capabilities.setCapability("network", true); capabilities.setCapability("devicelog", true); capabilities.setCapability("autoAcceptAlerts", true);

Handling location (i.e., allow once) pop-up from the Safari browser.

Since this pop-up is a part of the web view, you need to first switch the context in the Appium script & then accept the pop-up.

Below is the solution that also worked:

Step 1 - [Mandatory] Get all the contexts

Set<String> contextNames = driver.getContextHandles(); for (String contextName : contextNames) { System.out.println("Available contexts: " + contextName); }

Step 2 - [Optional] - Print the current context

Step 3 - [Mandatory] Switch to Native App context

driver.context((String) contextNames.toArray()[0]); String nativeContext = driver.getContext(); System.out.println("Switched to context: " + nativeContext);

Step 4 - [Mandatory] Now accept the pop-up by locating using the appropriate locator (User needs to change as per the requirement, though this should ideally work :))

// Change to XCUIElementTypeButton if XCUIElementTypeAlert does not work! driver.findElement(By.xpath("//XCUIElementTypeAlert[@name='Allow Once']")).click();

Step 5 - [Mandatory] Switch back to the older context

driver.context((String) contextNames.toArray()[1]); String webviewContext = driver.getContext();

You can refine the code as per the requirements.

Hope this helps @saanvi.savlani