Handling Alerts Using Appium



Handle Alerts API Demo
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example5AlertsAPIDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        DesiredCapabilities dc = new DesiredCapabilities();

        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");

        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android"); // Android Emulator

        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");

        // when we ara working on multiple apps we can just copy these lines of command
        // only thing we need to change is apk file location thats it
        dc.setCapability(MobileCapabilityType.APP, "C:\\ApiDemos.apk");

        URL url = new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver = new AndroidDriver(url, dc);

        driver.findElementsById("android:id/text1").get(2).click();// App

        driver.findElementsById("android:id/text1").get(3).click();// Alert Dialogs

       
        // Click on "OK Cancel dialog with a message" button.
        driver.findElementById("io.appium.android.apis:id/two_buttons").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.findElementById("android:id/button1").click();
       
        // Click on Cancel button of alert dialog.
        // driver.findElementById("android:id/button2").click();

        Thread.sleep(9500);
        driver.quit();

    }

}
Handle Alerts By Find Elements Class Name
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example5HandleAlertsByFindElementsClassName {

    public static void main(String[] args) throws MalformedURLException {
       

        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\AndroidUI.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       

        //Handle alerts
       
        driver.findElementById("com.android.androidui:id/buttonAlert").click();//click on link and opens alerts
       
        List alertbuttons=driver.findElementsByClassName("android.widget.Button");
       
        for(WebElement e:alertbuttons)
        {
            String text=e.getText();     //YES/NO
           
            if(text.equalsIgnoreCase("NO"))
            {
                e.click(); // click on alert button
                break;
            }
        }
       
       
    }
   

}

Handle Alerts By Id
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example5HandleAlertsById {

    public static void main(String[] args) throws MalformedURLException {
       
        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.APP, "C:\\AndroidUI.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       
        //Handle alerts
       
        //1st round
        driver.findElementById("com.android.androidui:id/buttonAlert").click();//click on link
       
        String alertmsg=driver.findElementById("android:id/message").getText();
        System.out.println(alertmsg);
       
        driver.findElementById("android:id/button2").click(); //click on No
       
        //2nd round
        driver.findElementById("com.android.androidui:id/buttonAlert").click();//click on link
       
        driver.findElementById("android:id/button1").click(); //click on Yes

    }

}


Popup API Demo

import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example5PopupAPIDemo {
   
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android"); //Android Emulator
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");

        //when we ara working on multiple apps we can just copy these lines of command only thing we need to change is apk file location thats it
        dc.setCapability(MobileCapabilityType.APP, "C:\\ApiDemos.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       
        driver.findElementsById("android:id/text1").get(10).click();//Views
       
        //capturing all the list items
        WebElement list=driver.findElementById("android:id/text1");
       
        //scrolling down till we get the element
        WebElement listitem =driver.findElement(
                MobileBy.AndroidUIAutomator(
                        "new UiScrollable(new UiSelector()).scrollIntoView("
                                + "new UiSelector().description(\"Popup Menu\"));"));
        listitem.click();
       
        driver.findElementByClassName("android.widget.Button").click();
       
        driver.findElementsById("android:id/title").get(1).click();
               
        Thread.sleep(9500);
        driver.quit();
       
    }
   
}

Followers