Test your Local Execution Mobile Appium Code.

You are here:
< All Topics
Overview

Appium is an open-source tool for automating mobile web, native, and hybrid applications on Android mobile, iOS mobile, and Windows desktops.

Appium is “cross-platform,” which means you can write tests for multiple platforms (iOS, Android, and Windows) using the same API. This allows for code reuse across iOS, Android, and Windows test suites.

With TestGrid, you can easily set up and test your mobile apps using your local Appium code for quicker results.

Prerequisites
  • TestGrid login credentials
  • Java Development Kit (JDK): Install the latest version of JDK on your machine. Appium is compatible with JDK 8 or later versions.
  • Integrated Development Environment (IDE): Choose an IDE to write and execute your Java Appium code. Popular choices include IntelliJ IDEA, Eclipse, or NetBeans. Make sure your IDE is properly installed and configured.
  • Appium Java Client: Add the Appium Java Client dependency to your Java project. Using a build automation tool like Maven or Gradle, you can include the dependency. The Appium Java Client allows you to interact with the Appium server using Java code.
  • There are client libraries in Java that support Appium’s WebDriver protocol extensions. You should use these client libraries instead of your regular WebDriver client when using Appium.

           Start configuring your local Java or Python code right away.

Step 1: Open the Device Cloud tab and locate the appropriate device information for running.
  • Login with TestGrid credentials and go to the Devices Cloud option tab.

 

 

Once the device’s cloud screen appears,

 

Step 2: Now select the appropriate device information for running local code on our remote device cloud.

 

 

Step 3:

Select any iOS or Android device on which you want to run it. You will find the required device appium capabilities.

 

Note: The below list of capabilities must be used for Android and iOS local execution with our device cloud.

# For Android

{ 
"appium:platformName": "Android",
 "appium:platformVersion": "12",
 "appium:deviceName": "Samsung Galaxy S10e",
 "appium:automationName": "UiAutomator2",
 "appium:udid": "R58M01147590X176B",
 "appium:systemPort": "37303",
 "tg:userToken": "58xzn0j3z48i3a247346286726on08g1jtmfic0ary4"
 }


# For iOS

{
"appium:platformName": "iOS",
"appium:platformVersion": "16.0",
"appium:deviceName": "iPhone 14",
"appium:udid": "00008110-001E05CC1ADA401E",
"browserName": "safari",
"appium:automationName": "XCUITest",
"appium:wdaLocalPort": "3802",
"tg:userToken": "pch78487ry8iuhsuskrxwei4ujverxb8w5jfxj"
}

Step 4: Steps to execute local Appium code.

Obtain the run-application remote URL and device capabilities from TestGrid-Device Cloud.

The following variables need to be changed as provided for the organization and as per devices:

  • TG_DEVICE_URL
  • TG_DEVICE_NAME
  • TG_DEVICE_UDID
  • TG_DEVICE_PLATFORMNAME
  • TG_DEVICE_PLATFORMVERSION
  • TG_DEVICE_SYSTEM_PORT (Android) *
  • TG_WDA_PORT (iOS) *
  • TG_USER_TOKEN

 

Notes: For authentication, the user may pass either the “tg: user token” capability or the “tg: userName” and “tg: password” capabilities.

// 1. Create an AppiumDriver

// 1.1 Set the capabilities of the driver

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Pixel 7");
capabilities.setCapability("platformVersion", "14");
capabilities.setCapability("platformName", "android");
capabilities.setCapability("automationName", "UiAutomator2");
capabilities.setCapability("udid", "< Device_UDID >");
capabilities.setCapability("tg:userToken", "< Your_User_token_here >");
capabilities.setCapability("systemPort", "4004");

// You can use this capability to categorise your test results.
capabilities.setCapability("tg:projectName", "AndriodDemoqa");
capabilities.setCapability("uiautomator2ServerLaunchTimeout", "90000");
driver = new AndroidDriver(new URL("http://{_YOUR_Domain_}/appium_38004/wd/hub"), capabilities);


// Java Syntax Add this at the start of your test to provide testcase name on Automation results screen.

((JavascriptExecutor) driver).executeScript(“tg:logs”, ImmutableMap.of(“testcaseName”, “SampleTestCase”));

//Add this after each step to add a screenshot for each step for visual testing.

((JavascriptExecutor) driver).executeScript(“tg:logs”, ImmutableMap.of(“testStepName”, “Step Description”));

Testcases pass or fail status can be added to the TestGrid Automation results screen by adding the following line of code:

//Pass

((JavascriptExecutor) driver).executeScript(“tg:action”, “{\”status\”: \”passed\”, \”case\”: \”Reason for the testcase!\”}”);

//Fail

((JavascriptExecutor) driver).executeScript(“tg:action”, “{\”status\”: \”failed\”, \”case\”: \”Reason for the testcase!\”}”);


Step 5: Determine the user’s Token capabilities.

a. Go to the Dashboard and then click on the Created “Test Runs Application”. After that, click on “Test Case Writer” and then click on “Versions.” On the same screen, look for the “i” button in the upper right corner.

 

b. Select the “i” button icon, and from there, obtain the user token.

 

For example, if you want to connect devices using the Appium Inspector tool,

 

To view the device inspector screen in Tools, click the “Start Session” button.

 

Step 6: Execute your local Appium code. Below is a sample for Java & Python.

 

For example, Android-Appium using JAVA.

 

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

public class myFirstAppiumJavaAndroid {

    public static myFirstAppiumJavaAndroid instance = new myFirstAppiumJavaAndroid();
    public AppiumDriver<?> driver;

    public void start() throws MalformedURLException {
        if (driver != null) {
            return;
        }

        DesiredCapabilities cap = new DesiredCapabilities();
        // The user can select the name of your plan based on the token id and the options (Free, Admin, OnPrem, POC, SubUser).
        cap.setCapability("tg:userToken", "< Your_User_token_here >");
        
        // If you are passing the above User token, there is no need to pass a username or password.
        // Your Email ID.
        cap.setCapability("tg:username", "< Your_Emails_ID >");

        // Your Password
        cap.setCapability("tg:password", "< Your_Password >");
        
        // Passed here Some required capabilities.
    	cap.setCapability("deviceName", "Moto G Play");
        cap.setCapability("udid", "4789ZY22CJCVK");
        cap.setCapability("platformVersion", "12");
        cap.setCapability("platformName", "Android");
        cap.setCapability("automationName", "UiAutomator2");
        cap.setCapability("systemPort","<_Your_Device_SystemPort_>");
        cap.setCapability("noReset", true);
        cap.setCapability("fullReset", false);
        cap.setCapability("autoGrantPermissions", true);
        cap.setCapability("tg:userToken", "put your usertoken here");
        
        // Your APP Packages
        cap.setCapability("appPackage", "com.android.chrome");
        cap.setCapability("appActivity", "com.google.android.apps.chrome.Main");

        // Your Remote DeviceCloud Public URL.
        URL url = new URL("http://{_YOUR_Domain_}.testgrid.io:39102/wd/hub");
        driver = new AndroidDriver(url, cap);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    public void stop() {
        if (driver != null) {
            driver.quit();
            driver = null;
        }
    }
}

 

For example, iOS-Appium using JAVA

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import io.appium.java_client.ios.IOSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

public class myFirstAppiumJavaiOS {

    public static myFirstAppiumJavaiOS instance = new myFirstAppiumJavaiOS();
    public AppiumDriver<?> driver;

    public void start() throws MalformedURLException {
        if (driver != null) {
            return;
        }
        String jpegPort = " < Your_jpegPort_No > ";

        // Passed here Some required capabilities.
        DesiredCapabilities cap = new DesiredCapabilities();

        // The user can select the name of your plan based on the token id and the options (Free, Admin, OnPrem, POC, SubUser).
        cap.setCapability("tg:userToken", "< Your_User_token_here >");

        // If you are passing the above User token, there is no need to pass a username or password.
        // Your Email ID.
        cap.setCapability("tg:username", "< Your_Emails_ID >");

        // Your Password
        cap.setCapability("tg:password", "< Your_Password >");

        // Passed here Some required capabilities.
        cap.setCapability("platformName", "iOS");
        cap.setCapability("automationName", "XCUITest");
        cap.setCapability("browserName", "safari");
        cap.setCapability("wdaLaunchTimeout", "9990000");
        cap.setCapability("deviceName", "iPhone XR");
        cap.setCapability("platformVersion", "14.7.1");
        cap.setCapability("appium:udid", "000080200000047474014402E");
        cap.setCapability("wdaLocalPort","<_Your_Device_wdaLocalPort_>");
        cap.setCapability("wdaConnectionTimeout", "9990000");
        cap.setCapability("tg:userToken", "put your usertoken here");

        // Passed Your Remote Device Public URL here.
        URL url = new URL("http://{_YOUR_Domain_}.testgrid.io:38001/wd/hub");
        driver = new IOSDriver<>(url, cap);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    public void stop() {
        if (driver != null) {
            try{
                driver.quit();
            }catch (Exception e)
            {
                System.out.println(e);
            }

            driver = null;
        }
    }
}

For example Python script for Android-Appium.

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Desired capabilities for the Android device
desired_caps = {
  "appium:platformName": "Android",
  "appium:platformVersion": "12",
  "appium:deviceName": "Samsung Galaxy S10e",
  "appium:automationName": "UiAutomator2",
  "appium:udid": "R58M90X178766B",
  "systemPort": "37303",
  "tg:userToken", "put your usertoken here",
  # If you are passing the above User token, there is no need to pass a username or password.
  "tg:username": "< Your_Emails_ID >",
  "tg:password": "< Your_Password >",
  # The user can select the name of your plan based on the token id and the options (Free, Admin, OnPrem, POC, SubUser).
  "tg:userToken": "< Your_User_token_here >"
}

# Appium server TestGrid Device Remote URL Here 
appium_url = 'http://demo.testgrid.io:37001/wd/hub'

# Initialize the driver
driver = webdriver.Remote(appium_url, desired_caps)

# Wait for the app to load
wait = WebDriverWait(driver, 10)
app_loaded = wait.until(EC.presence_of_element_located((MobileBy.ID, 'com.example.app:id/mainLayout')))
assert app_loaded is not None

# Perform actions on the app
element = driver.find_element(MobileBy.ID, 'com.example.app:id/button')
element.click()

# Swipe from one element to another
element1 = driver.find_element(MobileBy.ID, 'com.example.app:id/element1')
element2 = driver.find_element(MobileBy.ID, 'com.example.app:id/element2')
action = TouchAction(driver)
action.press(element1).move_to(element2).release().perform()

# Retrieve text from an element
text_element = driver.find_element(MobileBy.ID, 'com.example.app:id/textView')
text = text_element.text
print('Text:', text)

# Close the app
driver.quit()

 

For example Python script for iOS-Appium.

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Desired capabilities for the iOS device
desired_caps = {
  "appium:platformName": "iOS",
  "appium:platformVersion": "16.4",
  "appium:deviceName": "iPhone 8",
  "appium:udid": "e577127e8ef5383c0d8ff6daa",
  "appium:applicationName": "com.apple.mobilesafari",
  "appium:automationName": "XCUITest",
  "wdaLocalPort": "3606",
  "tg:userToken", "put your usertoken here",
  # If you are passing the above User token, there is no need to pass a username or password.
  "tg:username": "< Your_Emails_ID >",
  "tg:password": "< Your_Password >",
  # The user can select the name of your plan based on the token id and the options (Free, Admin, OnPrem, POC, SubUser).
  "tg:userToken": "< Your_User_token_here >"
}

# Appium server TestGrid Device Remote URL Here
appium_url = 'http://demo.testgrid.io:37001/wd/hub'

# Initialize the driver
driver = webdriver.Remote(appium_url, desired_caps)

# Wait for the app to load
wait = WebDriverWait(driver, 10)
app_loaded = wait.until(EC.presence_of_element_located((MobileBy.ID, 'com.example.app:id/mainLayout')))
assert app_loaded is not None

# Perform actions on the app
element = driver.find_element(MobileBy.ID, 'com.example.app:id/button')
element.click()

# Swipe from one element to another
element1 = driver.find_element(MobileBy.ID, 'com.example.app:id/element1')
element2 = driver.find_element(MobileBy.ID, 'com.example.app:id/element2')
action = TouchAction(driver)
action.press(element1).move_to(element2).release().perform()

# Retrieve text from an element
text_element = driver.find_element(MobileBy.ID, 'com.example.app:id/textView')
text = text_element.text
print('Text:', text)

# Close the app
driver.quit()

 

Step 7: View live results on the TestGrid Device cloud.

Additionally, the remote execution of code can also be viewed live on the TestGrid Device Cloud.

As simple as that! Happy Testing 😇

Please refer to the attached documents if you are using TestGrid One’s older Local Execution feature. : Executing-your-Local-appium-code

 

 

Table of Contents