Appium for Automating Native iOS Apps
RSSS

Appium Test Automation tool for Native iOS Applications

Guys, there has been some implementaion changes for the Appium tool and i am sure there will few changes in setting up the project as well as my blog post here, pls wait for the new updated post. Thanks for the patience

We have seen Selenium WebDriver’s -iPhoneDriver coming up lately for automation in iOS, but that is only for the browser’s and used only for testing web applications in iOS.

Here come’s the solution, Once again Jason Huggins is up on this new tool for automating the Native iOS applications on iOS devices.
Appium is a test automation tool for use with native and hybrid iOS applications.
It uses the webdriver JSON wire protocol to drive Apple’s UIAutomation.

Appium is based on [Dan Cuellar's](http://github.com/penguinho) work on iOS Auto. Just great, credit goes to him.

Dan’s Talk on Appium is here. Dont miss it, lovely to watch.

Some Good Reason’s to use Appium :

  • Appium is built on top of the Apple’s UI Automation framework and hence it is robust and no need for any code re-compilation of your app to use them.
  • Appium allows us to write tests in programming languages of our choice using Selenium WebDriver API and language specific client libraries.
  • Getting Started with Appium

    Step 1: The First step would be to clone the repository at https://github.com/hugs/appium

    Step 2: On your Terminal navigate to the cloned Appium directory and install dependencies using the command [sourcecode language="python"]pip install -r requirements.txt[/sourcecode]. It will start installing dependencies and looks like, as in the sceenshot
    Screen Shot 2013-01-14 at 1.09.55 AM

    Step 3: To avoid a security dialog that can appear when launching your iOS app, you need to modify your /etc/authorization file.(“at own risk” is what claimed by the developer’s.) Using the command [sourcecode language="python"]sudo python authorize.py[/sourcecode] and it will look like, as in the screenshot below
    Screen Shot 2013-01-14 at 1.06.37 AM

    Step 4: To launch an interpreter for sending raw UIAutomation javascript commands run:
    For Actual Device : Use the command[sourcecode language="python"]python appium.py com.yourApps.BundleID -U <DEVICE_UDID>[/sourcecode]
    For the Simulator:[sourcecode language="python"]python appium.py "path_to_your_ios_.app"[/sourcecode]If your app is already installed then you can find somewhere in Finder here and the link goes like – /Users/”Your Home NAme “/Library/Application Support/iPhone Simulator/Which version you use(5.1 “this is mine”)/Applications

    It will look like, as in the below screenshot (I have just tried to locate the iWebDriver app which i used for iPhoneDriver – Use your actual iOS App)

    Screen Shot 2013-01-14 at 1.43.01 AM

    We are done with the Steps to setup the Appium framework. Now Let’s see how to run the tests using WebDriver

    How to Run the Sample Scripts?

    JavaScript Approach

    Step 1: To run the test you must build the sample app (`sample-code/apps/TestApp/TestApp.xcodeproj`) in Xcode.Screen Shot 2013-01-14 at 1.51.54 AM

    Screen Shot 2013-01-14 at 1.52.49 AM
    Step 2: You can find the compiled app using spotlight from the command line.
    `mdfind -name TestApp.app`
    Screen Shot 2013-01-14 at 2.01.58 AM

    Step 3: Use that path to run the sample test `python js-test.py “/path/to/sample.app”`
    Screen Shot 2013-01-14 at 2.01.58 AM
    This program is the same, that you will find the cloned repository
    [sourcecode language="javascript"]import os
    import sys
    from random import randint

    # import appium
    appiumpath = os.path.abspath(os.path.join(os.path.split(os.path.abspath(__file__))[0],’../appium’))
    sys.path.append(appiumpath)
    import appium

    # generate two random numbers
    num1 = randint(1,9)
    num2 = randint(1,9)

    # create an appium instance
    driver = appium.Appium(sys.argv[1])
    driver.start()

    # enter the two numbers into the fields
    print ‘Entering "’ + str(num1) + ‘" into the first text field’
    driver.proxy(‘target.frontMostApp().mainWindow().textFields()[0].setValue("’ + str(num1) + ‘");’)
    print ‘Entering "’ + str(num2) + ‘" into the second text field’
    driver.proxy(‘target.frontMostApp().mainWindow().textFields()[1].setValue("’ + str(num2) + ‘");’)

    # submit the form
    print ‘Submitting the form’
    driver.proxy(‘target.frontMostApp().mainWindow().buttons()[0].tap();’)

    # validate the sum
    answer = driver.proxy(‘target.frontMostApp().mainWindow().staticTexts()[0].value()’)[0][1]
    correctanswer = num1 + num2
    if int(answer) is correctanswer:
    print ‘\033[92m' + 'SUM = ' + answer + '\033[0m'
    else:
    print '\033[91m' + 'SUM = ' + answer + ', it should be ' + str(correctanswer) + '\033[0m'

    # quit appium
    driver.stop()[/sourcecode]

    WebDriver Approach

    Step 1: Sample WebDriver Script that will computer 2 numbers in the Test App
    [sourcecode language="python"]from selenium import webdriver
    from random import randint

    # generate two random numbers
    num1 = randint(1,9)
    num2 = randint(1,9)

    # create a webdriver instance
    command_url = ‘http://localhost:4723/wd/hub’
    iphone = webdriver.DesiredCapabilities.IPHONE
    print ‘nconnecting to web driver @ ‘ + command_url
    driver = webdriver.Remote(command_url, iphone)

    # enter the two numbers into the fields
    fields = driver.find_elements_by_tag_name(‘textField’)
    print ‘Entering "’ + str(num1) + ‘" into the first text field’
    fields[0].send_keys(num1)
    print ‘Entering "’ + str(num2) + ‘" into the second text field’
    fields[1].send_keys(num2)

    # submit the form
    buttons = driver.find_elements_by_tag_name(‘button’)
    print ‘Submitting the form’
    buttons[0].click()

    # validate the sum
    labels = driver.find_elements_by_tag_name(‘staticText’)
    correctAnswer = num1 + num2
    displayedAnswer = labels[0].text
    if int(displayedAnswer) is correctAnswer:
    print ‘\033[92m' + 'SUM = ' + displayedAnswer + '\033[0m'
    else:
    print '\033[91m' + 'SUM = ' + displayedAnswer + ', it should be ' + str(correctAnswer) + '\033[0m'

    # quit the webdriver instance
    print 'Quitting webdrivern'
    driver.quit()
    [/sourcecode]
    Step 2: Compile and find the app as you did in the previous example and then launch the webdriver server. [sourcecode language="python"]python server.py "/path/to/sample.app"[/sourcecode]

    Screen Shot 2013-01-14 at 2.19.28 AM

    Step 3: Now you can run a test against that server. [sourcecode language="python"]python webdriver-test.py[/sourcecode]

    Few Questions to Answer :

    How to capture the Locators to use in the Scripts ?(Js approach)

    Instruments, which uses the UIAutomation library will be the answer. You can Map the target as and start recording the actions.

    How to get Instruments tool ?

    Instruments tool come’s with the Xcode developer tools. Install developer tools if you haven’t.

    instrments

    How to Map your App as a Target in Instruments ?

    Screen Shot 2013-01-14 at 8.01.54 PM

    Screen Shot 2013-01-14 at 8.17.42 PM
    We are done… :) If time permits I will take a real App and add a screencast of the same.

    Join and Post your questions on Appium

    15 thoughts on “Appium for Automating Native iOS Apps

    1. Nesta
      January 22, 2013 at 2:16 pm

      StumbleUpon brought me here! Great submittion!

    2. Z
      April 5, 2013 at 12:04 pm

      Hello,
      I am a newbie to mobile automation but appium got me very curious. Is there a way to come up with a screen capture video on how to setup and run a basic test against the test app? Greatly appreciate this in advance.
      Thanks

      1. April 5, 2013 at 12:15 pm

        Please use the Feedback page in the Home page to suggest .

    3. May 16, 2013 at 7:38 am

      Manoj,

      can we use Appium for testing Android Native apps?
      Also do you have a blog for the same on Java bindings?

      Any comment is welcome!!!

      Thank you!

      -Sams

      1. May 16, 2013 at 10:47 am

        Hi Prashanth,
        Yes Appium can be used for Native Android apps.

        I have used with android apk a while ago but at that time Appium was not that stable to work well.
        I believe now it should be stable by looking at the groups

        no blog posts on that yet, even this one is bit outdated… as it demo’s the Node js version.

        I will update when I find time.

        Thanks
        Manoj

      2. August 19, 2013 at 7:22 pm

        Hi Sams,

        checkout this link http://manojhans.blogspot.in/2013/08/native-android-apps-automation-with.html
        hoping it will fulfill your requirement.

        –Manoj Hans

    4. July 19, 2013 at 4:32 am

      Display pictures are not showing up in the tutorials.

      Please update them.

      Thanks

      1. July 23, 2013 at 6:38 pm

        Will resolve that immediately.

    5. Nikhil
      July 24, 2013 at 12:33 pm

      Can we also used Appium + selenium scripts against phonegap apps?
      any tutorial/docs on that ?

      1. August 4, 2013 at 6:59 pm

        We can use Appium for PhoneGap apps as well.
        As of now, from my side there is no tutorial in specific to phone gap apps with Appium.

    6. shilpa
      August 27, 2013 at 2:22 pm

      can anyone please tell me how do i setup appium Xcode and eclipse. I installed Node, Appium and Xcode.
      I have juint test in eclipse.i have to run web Juint test on Safari browser. i am unable to run the tests on safari.how do i run tests from eclipse.i started an appium server.. opened the simulator from Xcode.eclipse is not communicating with the simulator safari browser to run web tests..
      My code looks like this:

      import org.junit.After;
      import org.junit.Before;
      import org.junit.Test;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.remote.DesiredCapabilities;
      import org.openqa.selenium.remote.RemoteWebDriver;

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

      import static org.junit.Assert.*;

      /**
      *
      * Simple test which demonstrates how a test can be run against Mobile Safari running on an Appium instance.
      *
      * The test is based on https://github.com/appium/appium/blob/master/sample-code/examples/node/safari.js
      *
      * @author Ross Rowe
      */
      public class SafariTest {

      private WebDriver driver;

      /**
      * Instantiates the {@link #driver} instance by using DesiredCapabilities which specify the
      * ‘iPhone Simulator’ device and ‘safari’ app.
      * @throws Exception
      */
      @Before
      public void setUp() throws Exception {
      DesiredCapabilities capabilities = new DesiredCapabilities();
      capabilities.setCapability(“device”, “iPhone Simulator”);
      capabilities.setCapability(“version”, “6.1″);
      capabilities.setCapability(“app”, “safari”);
      driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub”),
      capabilities);
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }

      /**
      * Navigates to http://saucelabs.com/test/guinea-pig and interacts with the browser.
      *
      * @throws Exception
      */
      @Test
      public void runTest() throws Exception {
      driver.get(“http://saucelabs.com/test/guinea-pig”);
      WebElement idElement = driver.findElement(By.id(“i_am_an_id”));
      assertNotNull(idElement);
      assertEquals(idElement.getText(), “I am a div”);
      WebElement commentElement = driver.findElement(By.id(“comments”));
      assertNotNull(commentElement);
      commentElement.sendKeys(“This is an awesome comment”);
      WebElement submitElement = driver.findElement(By.id(“submit”));
      assertNotNull(submitElement);
      submitElement.click();
      WebElement yourCommentsElement = driver.findElement(By.id(“your_comments”));
      assertNotNull(yourCommentsElement);
      assertTrue(driver.findElement(By.id(“your_comments”)).getText().contains(“This is an awesome comment”));

      }

      /**
      * Closes the {@link #driver} instance.
      *
      * @throws Exception
      */
      @After
      public void tearDown() throws Exception {
      driver.quit();
      }
      }

    7. Kani
      November 6, 2013 at 4:29 am

      Can we use Appium on windows machine to test Iphone native and web apps ?

    8. Manju
      April 24, 2014 at 10:15 am

      Hi ,

      I would like to automate android application. I am comfortable in selenium. How can I set up environment? I am new in mobile application testing.

    9. Pingback: Shana Antunez

    Leave a Reply

    Your email address will not be published. Required fields are marked *