Appium Tip #9: How to Automatically Dismiss Dialogs and Auto-Accept Alerts

  November 09, 2015

In this blog of our Things You Should Know About Appium series, we’ll talk about how to use automatically dismiss and accept dialogs and alerts with Appium test scripts.

One of those annoyances for mobile app testing using whatever test automation framework, real devices and/or real browsers, and different versions of Android and/or iOS platforms have been those automatic dialogs by operating system platform. When your test script is going through your app and doing its work to hammer those specific test details, a notification from OS might suddenly pop up. What to do and how to get rid of those and proceed with the execution?

System Notification Problem with Test Automation and Testing

Notifications have been a discussed topic both among Android and iOS developers. Basically this feature that informs something about OS level – or just a push notification from a certain app – can make test automation scripts work flaky sometimes. Naturally, there are workarounds and ways to get rid of that notification, but however we do not recommend using any blockers (or blocker apps) as those may have an impact on OS, execution and even performance of your test session. The easiest way to ‘get rid of’ these dialogs, notifications or popups is to generate an auto-accept click either by a run-time click or on an image recognized event. We’ll discuss approaches here. At the same time if you don’t have a proper Appium environment, download our free Appium beginner’s guide and follow the instructions there to locate and fix the problems.

Appium Desired Capability to AutoAccept Dialogs and Alerts

With Appium, and especially on iOS, automating those OS level popups is problematic. On iOS, the permission alerts are system elements rather than something inside the automated app itself, which means you cannot create test steps to handle them. Those popups, notifications and dialogs may occur at random and must be handled instantly after test script sees those.

Appium has a simple solution to this. To solve this problem, you can use Appium’s desired capability specifically designed to handle these dialogs and alerts.

You can either always accept or always dismiss the alerts with these desired capabilities:

autoAcceptAlerts = true
...
capabilities.SetCapability("autoAcceptAlerts", true);

or

autoDismissAlerts = true
...
capabilities.SetCapability("autoDismissAlerts", true);

Furthermore, some of the older versions of Appium haven’t worked with this solution, so you might want to try a small workaround with this:

driver.SwitchTo().Alert().Accept();

Does Your Test Script Need Tweaks to Auto Accept/Dismiss?

To get these auto-accept/auto-dismiss desired capabilities working with your test script and applications, you should definitely experiment which one works better (and faster) so that application or test script execution doesn’t slow down because of these auto-accept/dismiss capabilities.

On Android, pop-ups can be automated, since automation is not restricted only to one app at a time. The problem, though, might be how to identify the pop-up on the screen. If a pop-up appears at the launch of your tested app, Appium inspector, for example, might not be able to bring out the element tree with the information. To solve this, you should use driver.getPageSource(). This command outputs the complete DOM, or Document Object Model, tree, which you can use to see all existing elements on the device. With this, it should be simple to find any pop-up button elements and type them into your automated script as a test step.

Using Image Recognition with Appium to Get Rid of System Alerts

It’s quite common that these system notification dialogs pop up both on Android and iOS – for various reasons. One of the great workaround to get click done instantly on these notification dialogs is to trigger image recognition feature for the execution. When a certain visual element or even part of the visual element is shown on the screen, the script will trigger a click on “OK”. For example, with the standard Android notification it can be recognized from this visual notification:

With iOS notifications, there are typically two buttons to select an activity for approving and getting rid of that notification. The image recognition example can work the same way and do the click for you (and for your actual test script). Typically on iOS, there are two

appium dialogs alerts on ios-1
appium dialogs alerts on ios-2

If you are interested to learn how image recognition can help in these kinds of system notification popups, take a look at this blog.

We’ll be continuing next week with a new topic so stay hungry and keep testing your mobile apps!

Happy Appium Testing!