How to connect IOS Device in Appium

  

To connect an iOS device with Appium for mobile automation testing, you need to set up your environment properly. Here's a step-by-step explanation:


System Requirements

RequirementDetails
OSmacOS (Appium for iOS requires Xcode)
iOS DeviceReal device with Developer Mode enabled
XcodeInstalled from Mac App Store
AppiumInstall via NPM or App
Node.jsRequired for Appium
CarthageRequired for WebDriverAgent
USB CableTo connect your iOS device
Apple Developer IDFor signing WebDriverAgent




Step-by-Step Setup

1. Install Prerequisites

Open Terminal and install the following:

brew install carthage
npm install -g appium
npm install -g ios-deploy



2. Enable Developer Mode on iPhone

  • Plug in the iPhone.

  • On the phone, tap Trust this computer.

  • Open Xcode > Devices and Simulators and ensure your device appears.



3. Create and Build WebDriverAgent

WebDriverAgent (WDA) is used by Appium to interact with iOS.

  • Clone WebDriverAgent or use Appium’s default one located at:

/usr/local/lib/node_modules/appium/node_modules/appium-webdriveragent
     
  • Open WebDriverAgent.xcodeproj in Xcode.
  • Set Development Team under Signing & Capabilities for both:

    • WebDriverAgentLib

    • WebDriverAgentRunner

  • Select your device as the build target.

  • Build the project using Xcode (⌘ + B).




4. Start Appium Server

Use terminal or Appium Desktop to start the server:

appium

You can also start it with custom settings:


appium --port 4723





5. Desired Capabilities (Sample)


{
  "platformName": "iOS",
  "platformVersion": "16.0",
  "deviceName": "iPhone 13",
  "udid": "<your_device_udid>",
  "automationName": "XCUITest",
  "bundleId": "com.apple.Preferences",  // or your app's bundle id
  "xcodeOrgId": "<your_team_id>",
  "xcodeSigningId": "iPhone Developer",
  "updatedWDABundleId": "com.yourcompany.WebDriverAgentRunner"
}




To get UDID, use:

idevice_id -l





Step 6: Java Code to Connect iOS Device in Appium


import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;

public class IOSAppiumTest {
    public static void main(String[] args) {
        try {
            DesiredCapabilities caps = new DesiredCapabilities();

            caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
            caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "16.0"); // your iOS version
            caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 13"); // or "iPhone"
            caps.setCapability(MobileCapabilityType.UDID, "your_device_udid_here");
            caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
            caps.setCapability(IOSMobileCapabilityType.BUNDLE_ID, "com.apple.Preferences"); // Open Settings app
            caps.setCapability("xcodeOrgId", "YOUR_TEAM_ID_HERE"); // from Apple Developer account
            caps.setCapability("xcodeSigningId", "iPhone Developer");
            caps.setCapability("updatedWDABundleId", "com.yourcompany.WebDriverAgentRunner");

            // Start Appium session
            IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);

            // Example: Interact with the Settings app
            System.out.println("App launched on iOS device successfully!");

            // Always quit the driver after test
            driver.quit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}






Maven Depenencies:


<dependencies>
    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>8.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.12.0</version>
    </dependency>
</dependencies>