Executing Appium Code from Windows Machine to Remote iOS Device

You are here:
< All Topics

Overview

Appium is most prevalently used for testing mobile apps. In order to get quicker and more precise results, it aids in running automated testing of native and hybrid applications for iOS devices.

Appium is a framework for automating the testing of apps that run on operating systems like iOS, macOS, Android, and Windows. Similar to Selenium, it gives the tester the ability to create test scripts in a variety of programming languages, including Java, Ruby, Python, PHP, and C#, which can be used on both Android and iOS platforms.

Prerequisites

  • Download the latest Appium client.
  • TestGrid account

How to Execute Local Appium Code from Windows Machine to Remote iOS Device?

Here is the Appium code for your reference:

For an example JAVA for iOS-Appium

package com.test.ios;

import io.appium.java_client.MobileElement;
import io.appium.java_client.ios.IOSDriver;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class iOSTest {
    public static void main(String args[]) throws IOException {
        IOSDriver driver = null;

        try {
            // -iOS Device capability as per metion TG device cloud
            DesiredCapabilities capabilities1 = new DesiredCapabilities();
            capabilities1.setCapability("platformVersion", "14.2");
            capabilities1.setCapability("bundleId", " <Bundle_ID> ");
            capabilities1.setCapability("deviceName", "iPhone 12 Pro Max");
            capabilities1.setCapability("platformName", "iOS");
            capabilities1.setCapability("automationName", "XCUITest");
            capabilities1.setCapability("udid", "00008101-001870C01E");
            capabilities1.setCapability("wdaLocalPort", 3606);
            capabilities1.setCapability("tg:userToken": "ochskrxwei4ujverxb8w5jfxj1bgnlsw");

            // Change below remote URL as per device cloud
            driver = new IOSDriver < MobileElement > (new URL("http://demo.testgrid.io:37001/wd/hub"), capabilities1);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            Thread.sleep(3000);

        } catch (MalformedURLException | InterruptedException e) {
            e.printStackTrace();
        }

        MobileElement elements = (MobileElement) driver.findElementByXPath("//XCUIElementTypeButton[@label='Add / Cancel']");
        elements.click();
        try {
            Thread.sleep(90000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            driver.getPageSource();
        }
    }
}

For an 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": "ochskrxwei4ujverxb8w5jfxj1bgnlsw"),
}

# 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()

 

  • Get Run URL and device compatibility from TestGrid’s device cloud.
  • Add the obtained URL to the java code.
public class MobileDemo{
  // Change below remote URL as per device cloud 
     driver = new IOSDriver < MobileElement > (new URL("http://demo.testgrid.io:37001/wd/hub"),

 

  • Add the obtained device capabilities to the code
  • The following variables need 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
    • TG_WDA_PORT (iOS) *
  • Now, set the package name and activity for the application that you want to automate.
    # 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": "ochskrxwei4ujverxb8w5jfxj1bgnlsw"
    }

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

          You can view the live execution results on the cloud portal of TestGrid.

         Additional Links

         You can also do these with the TestGrid Platform:

Table of Contents