Appium for Automating Native iOS Apps

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

    4 thoughts on “Appium for Automating Native iOS Apps

    1. 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

    Leave a Comment