How To Handle Android System Dialog


While automating an Android application a system Android dialog pops up. It is not available in the DOM tree and no actions against it are recorded in the test. How such a dialog could be handled?


The system dialog generated by the OS is not part of the application under test and thus is not present in the DOM tree. This is why there are no actions recorded against it.

Please find a coded approach how to handle such a dialog. The solution uses two bat files to execute the necessary commands through adb (Android Debug Bridge), a working directory (which is set to "C:\Work\" for this example but any other could be used after adjusting it in the code) and an xml file generated during the test run which contains all elements on the execution device screen. Once the file is copied to the working directory it is parsed and the required button on screen is located by its name. Its center could be located on screen using its coordinates and a tap is sent through adb.

Create a bat file named getdump.cmd and add the following adb commands in it:

adb shell uiautomator dump /data/local/tmp/test.xml

adb pull /data/local/tmp/test.xml

The first one will get all elements on screen and will save them into "test.xml" file which initially will be stored on the device. The second command pulls the generated xml on the desktop working direcotiry.

Create a second bat file named dismiss.cmd and add the following adb command:

adb shell input tap %1 %2

This is the tap on screen on specific coordinates sent through adb.

The sample project which uses the above two files and performs the parcing of the xml file, locates the required button on device screen and tap on it could be downloaded here.

Note: The above solution is a sample which demonstrates the approach how to handle system Android dialogs and will require adjustment for the specific dialog you need to automate.

In this article
Not finding the help you need? Improve this article