Automating iOS Native App Testing using UI Automation

In a nutshell, UI Automation is a tool that comes with MacOS X and the iPhone SDK/Xcode that uses Javascript as the scripting language.

Adi Saxena has an article on codeproject.com with a very good step by step guide to using this tool (see http://www.codeproject.com/KB/iPhone/UI_Automation_Testing.aspx).

This site has a very good tutorial on the tool – http://blog.manbolo.com/2012/04/08/ios-automated-tests-with-uiautomation.

Altf also has an article on this here http://altf.wordpress.com/2010/11/14/automating-ios-user-interface-testing-with-uiautomation/ .

I will be using Adi’s article as reference to explain the steps.

Step 1: Open Instruments and select Automation

Use Spotlight to to search for it. What’s Spotlight? On the top righthand corner of your Mac you’ll see a magnifying glass icon. Click on it and search for “Instruments”. Click on that. You will see the Instruments app show up.

Next click on “Automation” icon.

Instruments

Instruments

Step 2: Select Target

Next step is to select the target app. On the top left corner you will see a drop down labelled “Choose Target”. Select “Choose Target”.

Choose Target

Choose Target

Select the target app by navigating to it. You should be able to locate the app under the “build/Debug-iphonesimulator” folder.

If you wish to try out the tool, the codeproject site has a sample app with test JS.

Step 3: Run Script

On the lower left hand corner there is a “Choose script .. ” option. Use that to load the test JS script. We will deconstruct the JS code later.

Next click on the “Record” button – the one with the red dot. You will see the iPhone simulator pop up loading the app and the steps of the JS script will be run.

UI Automation tool with iOS Simulator activated and JS script run

UI Automation tool with iOS Simulator activated and JS script run

Deconstructing the JS test code

I’ll be using Adi’s JS code to explain how the tests work – so we’ll be referencing the code from the codeproject article.


// Get the handle of applications main window
var window = UIATarget.localTarget().frontMostApp().mainWindow();

As described in the comment – it’s to obtain the handle for the main window of the app.


var textfields = window.textFields();

This gets the array of text fields from the main window of the app.


// Check number of Text field(s)
if(textfields.length!=1)
{
UIALogger.logFail("FAIL: Inavlid number of Text field(s)");
}
else
{
UIALogger.logPass("PASS: Correct number of Text field(s)");
}

This test checks if the number of text fields is correct. The UIALogger JS logger object is used by UI Automation (hence the name) to log pass or fail cases logPass and logFail.


//TESTCASE_001 : Test Log on Screen
//Check existence of desired TextField On UIScreen
if(textfields["username"]==null || textfields["username"].toString() ==
"[object UIAElementNil]")
{
UIALogger.logFail("FAIL:Desired textfield not found.");
}
else
{
UIALogger.logPass("PASS: Desired UITextField is available");
}


This checks to see if the textfield is the correct one showing up.

Note textfields["username"]. What does “username” refer to? This is the accessibility label of the textfield UI object. If you open Interface Builder on Xcode (or IB) by clicking on the .xib file of the app’s Xcode project – you’ll be able to find it.


textfields["username"].setValue("username");
passwordfields[0].setValue("password");
buttons["logon"].tap();

These lines of code basically fills in the input fields with accessibility labels “username”, “password” and selects button with accessibility label “logon” and taps on the button.

Advertisements

2 Comments

Filed under iOS, Native App, QA, Testing

2 responses to “Automating iOS Native App Testing using UI Automation

  1. tranailinh

    Thanks for posting this helpful topic. But I have the question about “Step 2: Select Target”. How/Where do I get “build/Debug-iphonesimulator” folder ?
    Linh

  2. That’s a very good question. You should be able to find it in $HOME/Library/Developer/Xcode/DerivedData. You’ll need to drill down a little more though.

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