Appium - How To Handle Alert Dialog Of Android App

Alert dialog Is common element of any android software app so you must know how to handle android app alert In appium or how to click on OK button of android app alert In appium automation test . You can get alert when you click button or check the check box to get confirmation. Alert dialog contains OK and Cancel buttons and you can click on OK or cancel button. Right now, There Is not any Implementation of alert In AndroidDriver. So we need to find some alternative to handle alert In android appium software automation test. We can treat alert same as other elements In our android appium software automation test.

App To Use In Test
We will use API Demos android software app In this test. You can download API Demos software app from THIS PAGE. Install API Demos app In your android device.

Aim To Achieve In Appium Android Test
We Wants to click on OK or Cancel button of alert In this appium android software automation test as shown In bellow Image.

Manually you can navigate to above alert dialog screen from Open API Demos app -> Tap on App -> Tap on Alert Dialogs -> Tap on OK Cancel dialog with a message button.

Create And Run Appium Android Test To Handle Alert
Create new file under Android package of your project and copy paste bellow given software automation test script In It.
package Android;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class HandleAlert {
 AndroidDriver driver; 

 public void setUp() throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "");
  driver = new AndroidDriver(new URL(""), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

 public void okOnAlert() {
  // Scroll till element which contains "App" text.
  // Click on App.
  // Scroll till element which contains "Alert Dialogs" text.
  driver.scrollTo("Alert Dialogs");
  // Click on Alert Dialogs.
  driver.findElement("Alert Dialogs")).click();
  // Click on "OK Cancel dialog with a message" button.
  driver.findElement("OK Cancel dialog with a message")).click();
  // Get the text from alert dialog.
  String result = driver.findElementById("android:id/alertTitle").getText();
  System.out.println("Alert text Is -> " + result);
  // Click on OK button of alert dialog.
  // Click on Cancel button of alert dialog.
  // driver.findElement("Cancel")).click();

 public void End() {

Android Appium Test Description
Comments are provided with every statement In above android appium test script. Selenium has Alert class to handle alerts of web application. But It still Alert class Is not Implemented with AndroidDriver so we can not use It In our test. Here we have located Alert text container and OK button manually. Also you can click on Cancel button of alert using last statement.

This Is the way to handle alert dialog In android appium test.


  1. Scroll to is not working.can u provide work around

    1. I used this one then it worked very well. You can try it.

      MobileElement element= (MobileElement) driver

      .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"

      + ".resourceId(\".....")).scrollIntoView("

      + "new UiSelector().text(\"App\"));");;

  2. Hi ,

    Thanks for your article. I have been trying to handle an alert dialog. Unfortunately, UIAutomator can't identify the alert title or OK button. How can I handle such alert. Would you please help me?

    Thanks again!

  3. Use acceptalert Desriredcapabilites to yes