How to Setup and Automate iOS mobile application using Appium in Mac?

 

Work Flow of Appium

As discussed in the “How to Automate Android Mobile Application using Appium?” posting, Appium test script written in IDE will interact with the Appium Server which is nothing but the node server with the specified IP address and port number. Node server again passes the request to mobile devices or emulator using the uiautomation in Mac. All the UI elements associated with the mobile application can be controlled by using the appium client which is nothing but the derived one of selenium.

The following information is going to explain about the iOS application automation using Appium + TestNG + Core Java in Mac. Please note, this posting going to explain the iOS automation using the .APP file generated from the xcode project and all the automation testing happened using iPhone simulator.

How to setup Appium in Mac?

The following pre-requisites make it available at your end before start setting up Appium in Mac;

  1. Latest JDK (Pre-installed in most of the Mac)
  2. Appium.dmg for Mac
  3. Latest Eclipse IDE for Mac
  4. Source code of iOS app (xcode project)

Once the above pre-requisite is ready, start writing the test scripts in the newly created class file under java project by mapping the external dependencies of JAR files (Selenium & Appium client)

Now, we can see the above pre-requisite information in an elaborate way;

Pre-requisite 1:

Ensure the Java installation in the command prompt by entering java -version based on the below screenshot;

Java Installation Verification

Pre-requisite 2:

Download the Appium Installer from https://bitbucket.org/appium/appium.app/downloads/ and install the dmg file in your Mac;

Ensure Appium is successfully installed and running based on the below screenshot;

Appium

Ensure the below settings in the Appium server;

General settings of Appium server

Pre-requisite 3:

Download and install the Eclipse IDE from http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/marsr

TestNG should be downloaded from Eclipse market place and integrate with Eclipse IDE based on the below screenshot;

Go to help menu in Eclipse IDE and install the TestNG from Available software option;

How to integrate TestNG with Eclipse

Pre-requisite 4:

Get the source code(sample project) from the github using the URL https://github.com/appium/ios-uicatalog and download the zip file;

  • Unzip the file and click the .xcodeproj file associated with the folder
  • Select the build deployment target based on the available SDK mapped in the xcode

Selecting deployment target in xcode

  • Select the appropriate simulator in XCODE IDE to run the test application based on the below screenshot;

Selecting simulator in XCODE

Go to the terminal window available in the ‘Applications’ to build the source code without any errors and it will generate the .APP file for automating the same using Appium;

Go to the source code location in the terminal windows and build based on the following command as mentioned in the screenshot;

Generate app file

You will be getting the below succeeded message if there is no compilation or run time errors associated with the source code.

Build succeded

Ensure .APP file has been generated under the Downloads -> ios-uicatalog-master -> build -> Release-iphonesimulator

App file verification

All the pre-requisites are ready at our end and now coming to the script preparation for automating an iOS mobile application.

1. Create a java project in Eclipse IDE as AppiumDemo

New Java Project

2. Create a new package as appium.test(your own name) and class as AppiumTest(your own name) based on the below screenshot;

New Java class

3. Now add the script in the AppiumTest.java file in Eclipse IDE;

Before entering in to the script preparation, I would like to highlight the importance of Inspector option available in the Appium server;

It will help us to trace the mobile elements, which is used for triggering the user actions like click, enter text etc.

Inspector

Go to Eclipse IDE –> Project –> Properties –> Select ‘Java Build Path’ in left pane and select the ‘Libraries’ tab and select the ‘Add External Jars…’ to map all the dependencies jar as mentioned below;

Selenium supporting jars from http://www.seleniumhq.org/download/ (Java)

Appium client libraries jars from http://appium.io/downloads.html (Java)

Add the selenium-java-2.47.1.jar and all jar files available in the libs folder and enable the same based on the below screenshot.

Adding external libraries(JAR)

Selecting the JAR added in libraries tab

Everything is ready and we need to map the generated .APP file in the Appium server to trigger the user actions in the iOS app installed in the simulator.

Selecting .APP file in Appium Server

Launch the Appium server and it will start to run by invoking the iPhone simulator and the .APP file will be showing based on the below screenshot. Please quit the simulator in which while running script in Eclipse IDE, it will automatically invoke the simulator using Appium.

App in Simulator screen

Add the below script and run the test script in the Eclipse IDE;

package appium.test;

import io.appium.java_client.ios.IOSDriver;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class AppiumTest {
 
 
 @SuppressWarnings("rawtypes")
 private IOSDriver driver;
 
 @SuppressWarnings("rawtypes")
 @BeforeMethod
 public void setUp() throws MalformedURLException {
 DesiredCapabilities caps = new DesiredCapabilities();
 caps.setCapability("platformName", "iOS");
 caps.setCapability("platformVersion", "7.1"); 
 caps.setCapability("deviceName", "iPhone Simulator"); 
// caps.setCapability("bundleid", "com.example.apple-samplecode.UICatalog");
 caps.setCapability("app", "/Users/gopikannan/Downloads/ios-uicatalog-master/build/Release-iphonesimulator/UICatalog.app"); 
 driver = new IOSDriver(new URL("http://127.0.0.1:4725/wd/hub"), caps);
 }
 
 @Test
 public void testiOS() throws InterruptedException, IOException {
 driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]/UIAStaticText[1]")).click();
 driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]/UIAStaticText[1]")).click();
 driver.findElement(By.name("OK")).click();
 Thread.sleep(2000);
 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
 FileUtils.copyFile(scrFile, new File("/Users/gopikannan/Downloads/g2.jpg"));
 
 }
 
 @AfterMethod
 public void tearDown() {
 driver.quit();
 }
 
}

Go to Eclipse IDE and right click the mouse button in script window by selecting the ‘Run as’ option and again select the TestNG Test’ as mentioned in the below screenshot;

How to run test script

That’s it!!! You script will be executed in the mobile application installed in the iPhone Simulator.

Please check the below Post/URL to start automating the latest iOS 10 and above including xcode 8 and above;

https://gopekannan.wordpress.com/2017/08/26/how-to-setup-appium-in-mac-for-automating-ios-10-and-above/

36 thoughts on “How to Setup and Automate iOS mobile application using Appium in Mac?

  1. Hi ,

    I am getting an error “xcodebuild: error: ‘project.pbxproj’ is not a project file.” while trying to build the source code.Pls help

    Like

      1. I used the exact command that you mentioned in the blog.But when I tried building the app from exode itself I have got succesful build.

        But now I am facing issues while running the Javascript. Its giving an error saying element not found.
        Also in Appium, the application Bundle is not getting populated.How can we get this

        Like

  2. In this post UICatalog.app is used for execution. Is it possible to automate E-Commerce applications(ex: Flipkart)??
    If possible where can we find Source Code of application.

    Like

    1. Hi Avinash, You cannot get source code for flipkart or any other app unless it is available in github or some other open repository. We are having provision to break the android apk file to get the source code but the same provision is not available for iOS APP/IPA file due to it’s security limitation. Thanks

      Like

  3. Could not find a device to launch. You requested ‘iPhone (10.2 Simulator)’, but the available devices were: [“iPad Air (10.2) [AE98503C-F5E4-48B3-905F-541CE627BC19] (Simulator)”,”iPad Air 2 (10.2) [6652043B-664F-453E-9A33-393B51A79917] (Simulator)”,”iPad Pro (12.9 inch) (10.2) [A48F3CAE-9A38-48CD-AF1D-2E53B8AA977D] (Simulator)”,”iPad Pro (9.7 inch) (10.2) [478B8976-3277-4802-A0DC-B9ACCAB57CAB] (Simulator)”,”iPad Retina (10.2) [96E761C0-0DBF-4288-BC38-A56AB437DE02] (Simulator)”,”iPhone 5 (10.2) [B17E14A6-3F98-441D-A49E-1753AD1BEC29] (Simulator)”,”iPhone 5s (10.2) [FA2DAF81-DCCD-42FB-897E-A0550260252F] (Simulator)”,”iPhone 6 (10.2) [4F243CA5-696D-4E05-A5A4-52D821957400] (Simulator)”,”iPhone 6 Plus (10.2) [53901C70-1A70-4273-A592-F3A917475643] (Simulator)”,”iPhone 6s (10.2) [6C25324E-EA11-4514-8302-002FB61D2648] (Simulator)”,”iPhone 6s Plus (10.2) [E535C903-6C5F-4D6C-8CDB-6FF812E65DC8] (Simulator)”,”iPhone 7 (10.2) [405EBE73-329A-439F-B206-7C7CC03E6393] (Simulator)”,”iPhone 7 (10.2) + Apple Watch Series 2 – 38mm (3.1) [4EB80F2D-9CAC-4FF2-96D6-3A24F1026594] (Simulator)”,”iPhone 7 Plus (10.2) [B95CA6A3-B395-4E09-B9B8-5F87F29E829D] (Simulator)”,”iPhone 7 Plus (10.2) + Apple Watch Series 2 – 42mm (3.1) [CCD15041-FB54-4AF7-84CC-8A754E89C8DC] (Simulator)”,”iPhone SE (10.2) [9CEEED0C-3CC8-49FE-B91F-8D40E25AD574] (Simulator)”]

    How to deal with this? Thx

    Like

    1. Where you getting this exception? 1. While running your script in eclipse? If so share me your capabilities section OR 2. While try to build the source code to build .APP? 3. What is the xcode version you are using?

      Please tell me to proceed for further diagnosis.

      Like

    1. Yes, appium will launch the target mobile application in the simulator. Please note now the capabilities & code structure section has been changed for iOS > 9.3 onwards, I will post a new blog about the iOS 10 mobile application automation which will be using XCUITEST instead of UIAUTOMATION. Keep watching the space.

      Like

  4. Hi Gopi,

    info: [debug] [INST STDERR] Instruments Trace Error : Target failed to run: The operation couldn’t be completed. (FBSOpenApplicationErrorDomain error 1.) : Failed to launch process with bundle identifier

    I am getting this error on launching inspector and the app crashes on starting up.

    Please help me on this to proceed further 🙂

    Like

  5. Hi,
    I have installed appium 1.4.13 and my Mac OS version is 10.12.4. My appium check is successful.

    However, when I try to launch the appium for inspecting android app, I am unable to get the status. It remains unresponding. These are my logs:

    Launching Appium with command: ‘/Applications/Appium.app/Contents/Resources/node/bin/node’ lib/server/main.js –command-timeout “7200” –debug-log-spacing –automation-name “Appium” –platform-name “Android” –platform-version “4.4” –app “/Users/sandhya_velu/Downloads/My Knovel.apk” –app-pkg “com.elsevier.rnd.myknoveltogo” –app-activity “com.elsevier.rnd.myknoveltogo.MyKnovelToGo” –app-wait-package “com.elsevier.rnd.myknoveltogo” –device-name “07cd4a03”

    info: Welcome to Appium v1.4.13 (REV c75d8adcb66a75818a542fe1891a34260c21f76a)

    info: Appium REST http interface listener started on 0.0.0.0:4723

    info: [debug] Non-default server args: {“app”:”/Users/sandhya_velu/Downloads/My Knovel.apk”,”androidPackage”:”com.elsevier.rnd.myknoveltogo”,”androidActivity”:”com.elsevier.rnd.myknoveltogo.MyKnovelToGo”,”androidWaitPackage”:”com.elsevier.rnd.myknoveltogo”,”deviceName”:”07cd4a03″,”platformName”:”Android”,”platformVersion”:”4.4″,”automationName”:”Appium”,”defaultCommandTimeout”:7200,”debugLogSpacing”:true}

    info: Console LogLevel: debug

    It is not proceeding further. Please help me on this

    Like

    1. I have used latest version of appium 1.5.3, still I am unable to launch the server. Please find the server logs

      Launching Appium with command: ‘/Applications/Appium.app/Contents/Resources/node/bin/node’ appium/build/lib/main.js –debug-log-spacing –platform-version “8.1” –platform-name “iOS” –app “/Users/sandhya_velu/Downloads/ios-uicatalog-master/build/Release-iphonesimulator/UICatalog.app” –show-ios-log –default-device

      [Appium] Welcome to Appium v1.5.3

      [Appium] Non-default server args:

      [Appium] defaultDevice: true
      [Appium] debugLogSpacing: true
      [Appium] platformName: ‘iOS’
      [Appium] platformVersion: ‘8.1’
      [Appium] app: ‘/Users/sandhya_velu/Downloads/ios-uicatalog-master/build/Release-iphonesimulator/UICatalog.app’
      [Appium] showIOSLog: true
      [Appium] Deprecated server args:
      [Appium] –platform-name => –default-capabilities ‘{“platformName”:”iOS”}’
      [Appium] –platform-version => –default-capabilities ‘{“platformVersion”:”8.1″}’
      [Appium] –app => –default-capabilities ‘{“app”:”/Users/sandhya_velu/Downloads/ios-uicatalog-master/build/Release-iphonesimulator/UICatalog.app”}’
      [Appium] –show-ios-log => –default-capabilities ‘{“showIOSLog”:true}’
      [Appium] Default capabilities, which will be added to each request unless overridden by desired capabilities:

      [Appium] platformName: ‘iOS’

      [Appium] platformVersion: ‘8.1’
      [Appium] app: ‘/Users/sandhya_velu/Downloads/ios-uicatalog-master/build/Release-iphonesimulator/UICatalog.app’
      [Appium] showIOSLog: true

      [Appium] Appium REST http interface listener started on 0.0.0.0:4723

      Like

  6. Hi,
    My launching problem is solved. Able to launch successfully. Thanks 🙂

    However, appium 1.5.3 and Xcode 8.3 is not compatible. I downloaded “appium-xcuitest-driver-master”. I donno how to use it. Kindly help me on this

    Regards
    Sanu

    Like

      1. HI,
        Followed the above instructions and script executes successfully. Thank you! 🙂

        Also, I need steps to automate iOS application using ipa file. Kindly help me on this.

        Regards,
        Sanu

        Like

      2. Nice to see your script executed successfully in Mac.

        Instead of giving the .APP file location path, you just give the .IPA file location path in the “app” capability section.

        Ensure the simulator/iPhone device id information is loaded in the target .IPA file by the development team.

        Like

  7. Hi Gopi ,

    I am trying to run my test on Simulator .
    I am using this capabilities

    File app = new File(“/Users/xcodeclub/Desktop/UICatalog.app.zip”);

    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, “”);

    capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “8.1”);

    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “iPhone 6”);

    capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());

    IOSDriver driver = new IOSDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);

    I am getting error badparameter request from Appium

    Like

    1. Please follow the capabilities part as I mentioned in my blog page; You should give the .app file instead of .zip

      https://gopekannan.wordpress.com/2015/08/31/how-to-setup-and-automate-ios-mobile-application-using-appium-in-mac/

      @BeforeMethod
      public void setUp() throws MalformedURLException {
      DesiredCapabilities caps = new DesiredCapabilities();
      caps.setCapability(“platformName”, “iOS”);
      caps.setCapability(“platformVersion”, “7.1”);
      caps.setCapability(“deviceName”, “iPhone Simulator”);
      caps.setCapability(“app”, “/Users/gopikannan/Downloads/ios-uicatalog-master/build/Release-iphonesimulator/UICatalog.app”);
      driver = new IOSDriver(new URL(“http://127.0.0.1:4725/wd/hub”), caps);
      }

      Like

      1. I have tried with .app as well and did some other fixes as well however now app is not getting installed on my simulator and getting wd connection errors .after 15 mins trial I get error in eclipse that didn’t got response from server

        Thanks for your quick response

        Like

    1. Hi Ajit Gour,

      Unfortunately it will not support greater than iOS 9 incuding xcode > 8 but I will be posting same setup instructions for iOS 10 and Xcode 8+ before this ween end.

      Apple deprecated UIAutomation support from iOS 9+ onwards. Now XCUITest is the only way to automate the latest iOS with xcode 8+.

      Please keep watching.

      Like

  8. Hi Sir,
    by using Appium i am able to access my app but just want to open app using java code, i a getting the below exceptions please suggst me.

    FAILED CONFIGURATION: @BeforeMethod setUp
    org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4725 [/127.0.0.1] failed: Connection refused (Connection refused)
    Build info: version: ‘3.5.0’, revision: ‘8def36e068’, time: ‘2017-08-10T23:00:22.093Z’
    System info: host: ‘admins-iMac-2.local’, ip: ‘fe80:0:0:0:1c87:e13a:607a:c31%en0’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.12.6’, java.version: ‘1.8.0_144’
    Driver info: driver.version: IOSDriver
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:84)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:40)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.ios.IOSDriver.execute(IOSDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:254)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:137)
    at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:36)
    at io.appium.java_client.AppiumDriver.(AppiumDriver.java:114)
    at io.appium.java_client.AppiumDriver.(AppiumDriver.java:132)
    at io.appium.java_client.ios.IOSDriver.(IOSDriver.java:82)
    at Appium.AppiumTest.setUp(AppiumTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

    Like

    1. Appium Client not able to establish connection with the Appium Server based on the IP Address 127.0.0.1:4725

      Try to change the default IP address and port number as 0.0.0.0:4723 in Appium Server and start the Appium desktop server, now try to run your client specific code

      Ensure Appium console window showing the exception information as expected.

      Like

      1. But one more thing ,,,,,,,

        I want to run appium using cmd line.
        How i can ? Can you explain some?

        thanks i advance……

        Like

    2. Might be firewall or antivirus in your machine blocking the connection. Temporarily disable firewall and antivirus in your machine and try to run your Java code(Appium client)

      Like

Leave a comment