2/18/19

Handling Spinner Using Appium

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

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.remote.MobileCapabilityType;

public class Example12SpinnerApiDemo {
   
    public static void main(String[] args) throws Exception {
    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");

Handling Date Picker/Time Using Appium

 
Date Picker/Time
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 Example11DatePicker {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
       
        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\\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

Handling Expandable List Using Appium

Expandable List
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 Example10ExpandableList {

        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:\\apkfiles\\ApiDemos.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

Handling Text Box, Check Boxes & Radio Buttons Using Appium

Text Box, Check Boxes & Radio Buttons
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 Example9CheckboxRadioButtonTextField {

    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");

Handling Horizontal & Vertical Scrolling in Appium

Horizontal Scrolling
import java.net.MalformedURLException;
import java.net.URL;

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

import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.offset.ElementOption;

public class Example6HorizontalScrolling {

    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");

2/16/19

Handling Drag & Drop Using Appium

Drag And Drop Action
import java.net.MalformedURLException;
import java.net.URL;

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

import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.offset.ElementOption;

public class Example6DragAndDropAction {

    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\\more\\com.mobeta.android.demodslv-0.5.0-3_APKdot.com.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);

        //Navigation

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");

Handling Drop-down Using Appium

Handle Drop Down By 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 Example4HandleDropDownsByClassName {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        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");
       

Handling Switches Using Appium



Handle Switches Using Emulator
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 Example3HandleSwitchEmulator {

    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, 9.0);//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
       
        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);
       
       
        //Handling switch element
       
        //1 round
        driver.findElementById("com.android.androidui:id/mySwitch").click();//click on the switch
       
        String val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        //2nd round
       
        driver.findElementById("com.android.androidui:id/mySwitch").click();
       
        val=driver.findElementById("com.android.androidui:id/switchStatus").getText();
       
        System.out.println(val);
       
        driver.quit();
       

    }

}

Script For Install Apk File(App) On Real Device & Virtual Device

Required Desired Capabilities
DesiredCapabilities dc= new DesiredCapabilities();
dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");
dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
dc.setCapability(MobileCapabilityType.APP, "F:\\IndiaMART.apk");
Script for Install Apk file(App) on Emulator(Virtual Device)

 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;

2/14/19

Identify Elements Using Appium Inspector & Uiautomator


Steps to start Appium Inspector and inspect elements

  1. First of all, you need to start Appium GUI and then start a new session with appropriate desired capabilities for the AUT.
  2. A new window will open up, as shown in the above image.
  3. Now to locate the elements, do the following -
  4. Click on any element on the image on the left-hand side of the screen.
  5. After clicking, you will find the XML hierarchy of the app source displayed on the screen.
  6. On the right-hand side of the screen, you will find the attributes of the selected element like id or XPath of the element.
  7. You can now use your preferable method of finding the element, either by XPath or by id or by class.


Install APK(Mobile App) On Real Device using adb.exe

How to install apk file on Real device using adb.exe
  • Open the command prompt
  • Start server using adb.exe start-server
        C:\Users\admin>adb.exe kill-server
        C:\Users\admin>adb.exe start-server
  • Verify total devices using adb devices
        C:\Users\admin>adb.exe devices
  • Goto apk file directory, Run the below command to Install app.
         F:\Google Drive\Training Materials\Appium\APk Files>adb.exe install ApiDemos.apk

Install APK(Mobile App) On Real Device Using Appium Desktop

Vysor App
  • Vysor App is used for displaying mobile screens on your computer.
  • Vysor is available as a extension for Chrome. You have to install it.
  • And also available on Playstore. Download and install it.
  • Only for training purpose.