iOS Automation with Appium & Selenium

Note: This post is not up to date with latest release of Appium. An update coming soon…

Yesterday I saw a tweet on Appium release from Sauce Labs and immediately started exploring it. This post summarizes my initial experience with Appium.

Appium (http://appium.io/) is an open source tool/framework for automating iOS Native and Hybrid Apps. It uses the WebDriver JSON wire protocol to drive iOS apps.

Appium server is written in Node.js and talks to iOS using UIAutomation via Instruments. You can use the Selenium WebDriver API for writing tests which talk to Appium via JSON wire protocol for running the Selenium commands. This also gives you advantage of writing tests in your language of preference.

Installation

I found installing Appium quite easy on a local machine. You need Node.js installed before using Appium.

1. Install Node.js from http://nodejs.org/

2. Install WebDriver package for Node.js with the following command

sudo npm install wd

3. Install Appium with the following command

sudo npm install appium -g

4. Start the Appium server with the following command

appium &

Appium server will start at http://localhost:4723

Implementing test using Selenium WebDriver

I am using a sample BMI Calculator App developed with native iOS SDK for this example

Bmi Calculator App

Bmi Calculator App

Build the app using xcodebuild command (In this example the BmiCalc app)

xcodebuild -sdk iphonesimulator6.1

I am using Maven to setup a Java project for this test and here is pom.xml with the following dependencies added. For this example I have used IntelliJ IDEA. For more information on using Maven for Selenium script development refer bonus Chapter Integration with other Tools from my Selenium Testing Tools Cookbook

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.bmicalc.test</groupId>
    <artifactId>bmi-ios-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>LATEST</version>
        <scope>test</scope>
    </dependency>
    </dependencies>
</project>

And here is BmiCalcTest class

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import java.io.File;
import java.net.URL;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class BmiCalcTest {

    private WebDriver driver;

    @Before
    public void setUp() throws Exception {

        //Appium needs the path of app build
        //Set up the desired capabilities and pass the iOS SDK version and app path to Appium
        File app = new File("/Users/upgundecha/Desktop/AppExamples/BmiCalculator/build/Release-iphonesimulator/BmiCalculator.app");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
        capabilities.setCapability(CapabilityType.VERSION, "6.1");
        capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
        capabilities.setCapability("app", app.getAbsolutePath());

        //Create an instance of RemoteWebDriver and connect to the Appium server.
        //Appium will launch the BmiCalc App in iPhone Simulator using the configurations specified in Desired Capabilities
        driver = new RemoteWebDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
    }

    @Test
    public void testBmiCalc() throws Exception {

        //iOS controls are accessed through WebElement class
        //Locate the Height & Weight textField by their accessibility labels using By.name()
        WebElement heightTextField = driver.findElement(By.name("Height"));
        heightTextField.sendKeys("181");

        WebElement weightTextField = driver.findElement(By.name("Weight"));
        heightTextField.sendKeys("80");

        //Locate and tap on Calculate button using the click() method
        WebElement calculateButton =  driver.findElement(By.name("Calculate"));
        calculateButton.click();

        //Locate all the label elements using By.tagName()
        List<WebElement> labels = driver.findElements(By.tagName("staticText"));

        //Check the calculated Bmi and Category displayed on labels
        //Label with index 8 has value of the Bmi and index 9 has the value for category
        assertEquals("24.42",labels.get(8).getText());
        assertEquals("Normal",labels.get(9).getText());
    }

    @After
    public void tearDown() throws Exception {
        //Close the app and simulator
        driver.quit();
    }
}

I really liked using Selenium WebDriver API for writing iOS tests with Appium. I can add iOS support to my existing Selenium Framework with minimal changes. Appium presently supports locating elements using the tag name (i.e type of iOS control) and accessibility labels.

Running tests in Cloud

You can also run Appium with Sauce Labs Cloud, for more details read http://sauceio.com/index.php/2013/02/announcing-appium-on-sauce-native-hybrid-ios-testing-in-the-cloud/

Overall Appium is a great tool to start with.

References:
Getting Started – http://appium.io/getting-started.html
Appium on GitHub – http://sauceio.com/index.php/2013/02/announcing-appium-on-sauce-native-hybrid-ios-testing-in-the-cloud/
Samples – https://github.com/appium/appium/tree/master/sample-code
Wiki – https://github.com/appium/appium/wiki
Google Group – https://groups.google.com/forum/?hl=en&fromgroups=#!forum/appium-discuss


25 Comments on “iOS Automation with Appium & Selenium”

  1. sourishkrout says:

    Hi there! I’m a contributor on the Appium project and implemented large portions of the locators. There’s in fact a way to discover elements by their name (from Java this should be “By.Name()”). You should be able to either use the name of let’s say a botton (its caption, eg. “Calculate”) or if you have an accessibility label set (XCode) you can use this instead. Please note that if an accessibility label is set you will have to use it instead of the caption (UIAutomation detail).

    More comprehensive documentation to show up soon on Appium’s wiki: https://github.com/appium/appium/wiki

    Great blog post! Cheers,
    Sebastian

    • unmesh says:

      Thanks Sebastian!

      Yes, By.name() worked for me with accessibility label. Updated the example. However I observed that this does not apply to staticText. When I use getText() or getAttribute(“value”) methods, it returns the accessibility label text instead of the actual value on the staticText. Please advice.

      Best,
      Unmesh

  2. Z says:

    Hello,
    Firstly, I would like to thank you for the post on this. I am a newbie to this but topic of mobile automation is something that is going to be hot in the next few years since mobile market is getting huge. I was just curious if it would be possible to provide a more detailed walk through for beginners? I have installed appium and ran a build but I am not sure how to properly create a folder structure with java classes in xcode. Where pom.xml should be placed? How to run test scripts on the simulator/real device?
    I apologize if my questions are stupid, I am just seeking some help to get started.

    Thank you.
    Z

    • unmesh says:

      Hi Z,

      Glad you liked this post and thanks for the feedback. I have updated the post. I’ll be also posting a video tutorial on this soon.

      Best,
      Unmesh

  3. Anirudh says:

    Hi Unmesh,

    For the past 2 days I am trying to get an idea/implementation of appium but unable to get to an end to end flow.

    I installed appium app on my mac.
    And there is something that needs to be done in the xcode.. I checked out selenium project from here. https://code.google.com/p/selenium/source/checkout
    I opened the iwebdriver.xcodeproject file to setup the iwebdriver for the simulator/device but here I faced an issue and was unable to proceed from there on..

    Though, I have got a vague idea on this, I wasn’t able to get to an end to end flow… Hope I get some info from your end to reach my goal of automating a native app in both android & ios

    Appreciate your help!

    Regards,
    Anirudh

    • Anirudh says:

      Thanks for initiating this thread.

    • unmesh says:

      HI Anirudh,

      Once you install the Appium and get ready your application, you can drive Selenium by various ways. iWebDriver is different thing here. It is used to run web tests on iOS Device or Emulator.

      You need to setup Selenium with your choice of language. In this post I have used Java bindings for Selenium.

      I’ll be post more on this topic soon which will help you to evaluate and decide various ways you can use Appium, keep watching this space.

      Thanks,
      Unmesh

      • Anirudh says:

        Thanks Unmesh, will be waiting for your post.

        Also, is XCode required to run our tests through Appium?

        Regards,
        Anirudh

      • testmann says:

        HI Anirudh,

        You don’t need to use XCode to run Selenium tests on Appium (unless you’re developing tests in Objective-C). If Java is your choice for Selenium tests, then you can use IDE’s like Eclipse or IntelliJ IDEA to develop Selenium tests and run them with Appium.

        Best, Unmesh

  4. dprski33 says:

    Can you please provide some detail of how you intergrated appium into your existing selenium framework? Is all your testing done on macs?

    • unmesh says:

      Hi dprski33,

      I am writing further posts on how to bring Selenium and Appium together, keep watching this space.

      Yes, for the illustrated test in this post I have used Appium on Mac.

      Thanks,
      Unmesh

  5. Manish says:

    Hi unmesh,

    I am working on a html5 application and have been using Selenium for my multi-browser testing without any issues. Is it possible to integrate my existing selenium tests with appium and run my tests on multi-devices like IOS and Android platforms.

    Additionally, is it possible to set up Appium on a Windows application, as currently i am working on a windows platform.

    Please advise.

    Regards
    Manish

    • unmesh says:

      Hi Manish,

      Appium is built for testing native apps on both platforms (but nothing stops you to test Web/HTML5 application using WebView). If you want to test native apps, then I would recommend to use Appium.

      However for testing Web/HTML apps you can use IPhoneDriver and AndroidDriver in Selenium.

      Recently Appium team has released a Windows version.

      Best,
      Unmesh

      • jazz says:

        I’m confused! I came to Appium because the iPhoneDriver page says:

        “If you are looking to use WebDriver with iOS mobile Safari and are currently testing only on simulators please have a look at ios-driver or appium”

        “Both of these projects are much better implementations of WebDriver for iOS” and

        “iPhone Driver (DEPRECATED)”

        So what to use for HTML5 in mobile safari?

  6. James says:

    Hey Unmesh, where can I get the BMI app from. Also waiting for that video you said you’ll make. I wanted to run the exact scenario you are running and then move on to testing my app. Thank you.

  7. KOushik says:

    Hi Umesh ,

    Great work and good post. it’s working for me…. thank you …… can you share us , how to do native app testing on the Andriod OS ..

    Thanks

  8. […] First, install NodeJs & Appium […]

  9. Eku says:

    Hi Unmesh
    i am using the same code as you have written in this post. But when i run the program i get the below error :

    A new session could not be created. (Original error: Instruments crashed on startup)

    I am following the following steps:

    I launched the appium server.
    I have started the iPhone simulator as well.

    But when i run the test case overtime i get the same issue..

    Please help me to resolve the issue.
    as i am stuck on this.

    Thanks

  10. Shri says:

    Hi thanks for wonderful information. I needed some inputs to run the above code in eclipse. I get lots of error with import.Can u suggest me how to get rid of it.

  11. Pasquale Hal says:

    iOS Automation with Appium

    […]I got a new iphone 4 and ever sence I got it It will not alow me to send or receve picture messeges at all……. what can I do about it.[…]

  12. Andrew says:

    Can one do all this in Windows?

  13. ravi says:

    can anybody tell me how to use swipe method when we use webdriver for native iosapp

  14. kbmail says:

    Hi umesh, where can i find latest appium version (1.6 +) for mac os. I could not find this in appium downloads.

  15. filmywap apk says:

    I pay a quick visit every day a few websites and sites to
    read articles or reviews, except this web site offers feature based articles.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s