Click here to Skip to main content
15,311,216 members
Articles / DevOps / Testing
Posted 6 Sep 2010


16 bookmarked

How Do I Perform UI Automation Testing in iOS 4

Rate me:
Please Sign up or sign in to vote.
4.63/5 (7 votes)
7 Sep 2010CPOL3 min read
Beginner level guide (step by step) on UI Automation Testing in iOS 4


This is a beginner level article. I hope after going through it, a typical iPhone developer would be able to perform automated UI testing.


UI Automation testing is an important value addition in iOS4. It is supported by a new instrument object called “Automation”. It’s quite suitable for UI testing of Productivity style applications.

Automation instrument works from scripts (written in JavaScript). It simulates/fires required events on target Application. Test script must be a valid executable JavaScript file accessible to the instrument on the host computer.

What is a Test Script?

Test script is an ordered set of commands, each of which accesses a user interface element in application to perform a user action on it or to use the information associated within it.

In this article, I will explain UI Automation testing with the help of one sample application developed using Coco Touch framework.


My sample application has one screen called “Login”. It contains two text fields name as “User name” and “Password” and one Button called “Login”.

Before writing the test script, tag all your UI controls in “Interface Builder” with names, by setting the Accessibility label to a unique value for the view (It is a mandatory requirement).

Now compile your application in debug mode.

Test Scripts

As I mentioned above, test scripts are basically a set of ordered commends. In other words, it’s conversion of textual test cases into JavaScript which would be auto executed by “Automation” instrument.

Here is the sample test script:

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

// Get the handle of view 
var view = window.elements()[0]; 

var textfields = window.textFields(); 
var passwordfields = window.secureTextFields(); 
var buttons = window.buttons(); 
var textviews = window.textViews(); 
var statictexts = window.staticTexts(); 
var target = UIATarget.localTarget(); 

// Check number of Text field(s) 
   UIALogger.logFail("FAIL: Inavlid number of Text field(s)"); 
   UIALogger.logPass("PASS: Correct number of Text field(s)"); 
// Check number of Secure field(s) 
   UIALogger.logFail("FAIL: Inavlid number of Secure field(s)"); 
   UIALogger.logPass("PASS: Correct number of Secure field(s)"); 

// Check number of static field(s) 
   UIALogger.logFail("FAIL: Inavlid number of static field(s)"); 
   UIALogger.logPass("PASS: Correct number of static field(s)"); 

// Check number of buttons(s) 
   UIALogger.logFail("FAIL: Inavlid number of button(s)"); 
   UIALogger.logPass("PASS: Correct number of button(s)"); 

//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."); 
   UIALogger.logPass("PASS: Desired UITextField is available"); 

//TESTCASE_1.2 :Check existence desired of PasswordField On UIScreen 
if(passwordfields[0]==null || passwordfields[0].toString() == "[object UIAElementNil]") 
   UIALogger.logFail("FAIL:Desired UISecureField not found."); 
   UIALogger.logPass("PASS: Desired UISecureField is available"); 

//TESTCASE_1.3 :Check For Existence of Buttons On UIScreen 
if(buttons["logon"]==null || buttons["logon"].toString() == "[object UIAElementNil]") 
   UIALogger.logFail("FAIL:Desired UIButton not found."); 
   UIALogger.logPass("PASS: Desired UIButton is available"); 

//TESTCASE_001 : Missing User Name 



var errorVal=textviews["error"].value(); 
if(errorVal!="Invalid User Name or Password") 
   UIALogger.logFail("Did Not Get Missing UserName Error : "+errorVal); 
   UIALogger.logPass("Missing User Name"); 

//TESTCASE_002 : Missing Password 


var errorVal=textviews["error"].value(); 
if(errorVal!="Invalid User Name or Password") 
   UIALogger.logFail("Did Not Get Missing Password Error : "+errorVal); 
   UIALogger.logPass(" Missing Password"); 

//TESTCASE_003 : Successful Log On 

All UI elements in the application are represented to the script through an ordered hierarchy of objects defined by the UIAElements class and its subclasses like UIATarget, UIALogger, etc.

For more information, refer to the UI Automation Reference Collection available at Apple Developer site.

After completing the script, we’re all set for execution. Here are the steps:

Step 1

Open Instruments (You can find it is Spotlight). Select “Automation” from template selection window.

Step 2

It will open Trace Window. Here, you have to select debugging version of your application with the help of “Choose Target” pull down.

Step 3

Use “Script” pull down to select test script file and then click on “Run and Record” button.

It will auto launch “iPhone Simulator” and start executing the test scripts (this operation might take 4-5 seconds).

Reusable Test Scripts

The API offers a #import directive that allows you to write smaller, reusable discrete test scripts.

E.g. If you were to define commonly used functions in a file named TestUtilities.js, you could make those functions available for use in your test script TestXYZ.js by including in that script the line:

#import "<path-to-library-folder>/TestUtilities.js" 

Hardware Requirement to Try Out the Attached Sample Code

Mac Mini (Intel processor based) with Snow Leopard 10.6 (or above) and iOS SDK 4.0:

  1. Unzip attached “”, compile it in debug mode and test it on Simulator.
  2. “username/password” are valid credentials; enter these values in corresponding “user ID” and “Password” field.

Are You Facing "Unexpected error ...." ?

"Unexpected error in -[UIATarget_0x5a20d20 frontMostApp], 
/SourceCache/UIAutomation_Sim/UIAutomation-37/Framework/UIATargetElements.m line 437"

If you are facing this problem, this can be corrected by copying a to 4.0.1.

Copy to:

~/Library/Application Support/iPhone Simulator/4.0.1/Library/Preferences

Make sure that there should be only two Keys in this file name as “AccessibilityEnabled” and “ApplicationAccessibilityEnabled”. Both the keys should be checked.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Adi Saxena
Architect Impetus Technologies Inc.
India India
No Biography provided

Comments and Discussions

QuestionHow to access Settings bundle in iPhone using automation script Pin
Member 948628511-Dec-12 0:53
MemberMember 948628511-Dec-12 0:53 
Questionfail to compile (build) Pin
Member 857828118-Jan-12 9:27
MemberMember 857828118-Jan-12 9:27 
QuestionRe: fail to compile (build) Pin
AmolVJadhav15-May-12 20:34
MemberAmolVJadhav15-May-12 20:34 
QuestionCan we execute the similar tests in the actual instrument(Ipad/iphone)? Pin
hsupadrasta20-Oct-10 11:40
Memberhsupadrasta20-Oct-10 11:40 
AnswerRe: Can we execute the similar tests in the actual instrument(Ipad/iphone)? Pin
Adi Saxena28-Oct-10 20:45
MemberAdi Saxena28-Oct-10 20:45 
GeneralRe: Can we execute the similar tests in the actual instrument(Ipad/iphone)? Pin
Shmikkil9-Nov-10 6:06
MemberShmikkil9-Nov-10 6:06 
GeneralRe: Can we execute the similar tests in the actual instrument(Ipad/iphone)? [modified] Pin
Shmikkil9-Nov-10 7:47
MemberShmikkil9-Nov-10 7:47 
GeneralMy vote of 5 Pin
Tom The Cat16-Sep-10 6:09
MemberTom The Cat16-Sep-10 6:09 
GeneralRe: My vote of 5 Pin
Adi Saxena27-Sep-10 7:55
MemberAdi Saxena27-Sep-10 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.