Executing Your Local Appium Code

You are here:
< All Topics

Executing Local Appium Code

Following is the sample Appium code for reference:

package test;

import java.net.MalformedURLException;
import java.net.URL;

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

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;

public class MobileDemo{

    //Obtain Run URL from Link Provided for Organization
    static String TG_DEVICE_URL="https://demo.testgrid.devicecloud/wd/hub";
    
    //Obtain Capabilities from Link Provided for Organization
    static String TG_DEVICE_NAME="Pixel 3";
    static String TG_DEVICE_UDID="TGDC0001";
    static String TG_DEVICE_PLATFORMNAME="Android";
    static String TG_DEVICE_PLATFORMVERSION="10";
    
    //Set Package Name & Activity - For App You want to automate
    static String APP_PACKAGE="com.android.calculator2";
    static String APP_ACTIVITY="com.android.calculator2.Calculator";

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("deviceName",TG_DEVICE_NAME );
        cap.setCapability("udid", TG_DEVICE_UDID);
        cap.setCapability("platformName", TG_DEVICE_PLATFORMNAME);
        cap.setCapability("platformVersion",TG_DEVICE_PLATFORMVERSION);
        cap.setCapability("appPackage",APP_PACKAGE);
        cap.setCapability("appActivity", APP_ACTIVITY);
        URL url = new URL(TG_DEVICE_URL);
        AppiumDriver<MobileElement> driver = new AppiumDriver<MobileElement>(url,cap);
        
        //Write Automation Steps
        MobileElement no7 = driver.findElement(By.id("com.android.calculator2:id/digit_7"));
        MobileElement no3 = driver.findElement(By.id("com.android.calculator2:id/digit_3"));
        MobileElement plusop = driver.findElement(By.id("com.android.calculator2:id/op_add"));
        MobileElement eqop = driver.findElement(By.id("com.android.calculator2:id/eq"));
        no7.click();
        plusop.click();
        no3.click();
        eqop.click();
        driver.quit();	
        
    }
}

Following variables needs to be changed as provided for organization & as per devices:

  • TG_DEVICE_URL
  • TG_DEVICE_NAME
  • TG_DEVICE_UDID
  • TG_DEVICE_PLATFORMNAME
  • TG_DEVICE_PLATFORMVERSION

Additionally, Remote Execution of Code can also be viewed live on TestGrid Browser Cloud.

Table of Contents