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 HandleAlert.java under Android package of your project and copy paste bellow given software automation test script In It.

HandleAlert.java
package Android;

import io.appium.java_client.android.AndroidDriver;

import java.net.URL;
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; 

 @BeforeTest
 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", "io.appium.android.apis");
  capabilities.setCapability("appActivity","io.appium.android.apis.ApiDemos");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void okOnAlert() {
  // Scroll till element which contains "App" text.
  driver.scrollTo("App");
  // Click on App.
  driver.findElement(By.name("App")).click();
  // Scroll till element which contains "Alert Dialogs" text.
  driver.scrollTo("Alert Dialogs");
  // Click on Alert Dialogs.
  driver.findElement(By.name("Alert Dialogs")).click();
  // Click on "OK Cancel dialog with a message" button.
  driver.findElement(By.name("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.
  driver.findElement(By.name("OK")).click();
  // Click on Cancel button of alert dialog.
  // driver.findElement(By.name("Cancel")).click();
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}

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.

4 comments:

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

    ReplyDelete
    Replies
    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\"));");

      element.click();

      Delete
  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!

    ReplyDelete
  3. Use acceptalert Desriredcapabilites to yes

    ReplyDelete