Introduction to Automating UI Testing in iOS

This is a basic level tutorial, I hope after going through it, you will be able to perform an Automated UI Testing.

UI Automation testing is perform by “Automation” a new addition to instrument in iOS4 (so, these notes are for XCode 4). Automation instrument works with JavaScript so you will need a basic JavaScript skills. Apple provides a set of JavaScript libraries, that you can use to drive your tests and simulate user interaction.

First we will create a simple app and then the script for the Automating UI Testing. I’m using XCode 4.2 but the instructions are the same for XCode 4.1

     Create a Simple App for the Automating UI Testing
I will not be detailed in this part of tuturial, because what matters is the script and the Automation instrument

  1. Start XCode and create a new project “Single View Application“.
  2. Provide name and a location. For this example, I will use the name MyFirstUITest and the directory will be /Users/Nearsoft/Desktop, also I choose iPhone in the Device Family.
  3. Change the code in the MyFirstUITestViewController.h, it will see like the following:
    @interface MyFirstUITestViewController : UIViewController{
    IBOutlet UITextField *nameTextField;
    IBOutlet UILabel *greetingsLabel;
    }
    - (IBAction)sayHello:(id)sender;
    - (IBAction)clearAll:(id)sender;
    
    @end

    (will then have something like this)



  4. In the MyFirstUITestViewController.m add the following code for the IBActions:
    - (IBAction)sayHello:(id)sender {
        greetingsLabel.text =[NSString stringWithFormat:@"Hello %@", nameTextField.text];
    }
    - (IBAction)clearAll:(id)sender {
        greetingsLabel.text=@"";
        nameTextField.text=@"";
        [nameTextField resignFirstResponder];
    }
  5. In the MyFirstUITestViewController.xibadd the necessary UIObjects in the view, to look like the following image:

  6. Then connect the IBOutlets with their respective UIObjects in the view: the nameTextField IBOutlet with the UITextField and the greetingsLabel IBOutlet with the UILabel. Also connect the IBActions with their respective buttons in the view; sayHello IBAction with the Say Hello Button and the clearAll IBAction with the Clear All Button. Something like the image:

  7. Once you finished the IBOutlets and IBActions connections, compile an run your project. Is preferable to use the device(real iPhone), but if you don’t have the Apple DevProgram use the simulator. If you did everything right you can test the app and will see something like this,

  8. Now, quit the simulator or device and switch back to XCode.Create a the TestScript
  9. Back to XCode create a new Group inside the MyFirstUITest directory, I will use the name UIScripts

  10. Inside the UIScripts create a New File an Empty one. It will be our JavaScript file, I will use the name UITestScript.js

  11. Now open the UITestScript.js file and add the following code:
    // Setup
    var app= UIATarget.localTarget();
    var target = app.frontMostApp().mainWindow();
    
    //Define individual elements
    var nameTextField = target.textFields()[0];
    var sayHelloButton = target.buttons()[0];
    var regardsLabel = target.staticTexts()[0];
    
    var clearAllButton = target.buttons()[1];
    
    //Test functions
    function helloWorldTest(name){
    
        var testname = "Hello Word - Test";
    
        //Start the test
        UIALogger.logStart(testname);
    
        UIALogger.logMessage("Put the word into the UITextField");
        nameTextField.setValue(name);
    
        //If you don't want a delay,comment the following line
        app.delay(2);
    
        UIALogger.logMessage("Take scheenshot");
    
        //Take scheenshot for the current Name on UITextFiel
        //You can name it as you want
        app.captureScreenWithName("Scheenshot1");
    
        UIALogger.logMessage("Tap on SayHello Button");
        sayHelloButton.tap();
    
        //Read the Label
        UIALogger.logMessage("Read Label");
        var label = regardsLabel.value();
        if (label == "Hello World"){
            //the test ended with a successful outcome
            UIALogger.logPass(testname);
        }else{
            //the test ended with a unsuccessful outcome
            UIALogger.logFail(testname);
        }
    
    }
    
    function clearAll(){
    
        var testname = "Clear Fields";
    
        //Start the test
        UIALogger.logStart(testname);
        app.delay(2);
    
        UIALogger.logMessage("Tap on Clear All Button");
        clearAllButton.tap();
    
    	app.captureScreenWithName("Scheenshot2");
    	var label = regardsLabel.value();
    	var textbox = nameTextField.value();
    
        if (label == "" && textbox == ""){
            //the test ended with a successful outcome
            UIALogger.logPass(testname);
        }else{
            //the test ended with a unsuccessful outcome
            UIALogger.logFail(testname);
        }
    
    }
    
    //Calling Functions
    
    helloWorldTest("World");
    clearAll();

    All UIObject in the app are represented into the script according the ordered hierarchy in the view. In the past script we have just two  functions.

    • function helloWorldTest(name): It will receives a string and set into the UITextField and also Tap the “Say Hello” Button.
    • function clearAll():It will tap the “Clear All” button.

    I highly recommend see the Automating User Interface Testing with Instruments for understand better the script functionality and sintaxis.

     Perform the Automating UI Test

  1. Open Instruments /Developer/Applications/Instruments or you can use Spotlight. Then Select Automation

  2. If you compile the app in the device(real iPhone) in the “Choose Target” pull down, select you iPhone. If not select your Mac:

  3. Again in the  ”Choose Target” pull down, select the Choose Target option and then the MyFirstUITest app, just like the image bellow:

    if in the step 2 (of this section) you choose your Mac, you need look for the Target in the Simulator’s File System. It’s in the directory ~/Library/Application Support/iPhone Simulator/user/Applications/

  4. Just in the middle of the Automation window are the scripts section, click in the Add button and choose “Import…”. Look for your script at the MyFirstUITestin the Desktop

  5. Then click on “Record” button. It will auto launch the app in your iPhone or iPhone Simulator and start the test script.


And Finally..

Congratulations, we did it!.

Documentation:

9 thoughts on “Introduction to Automating UI Testing in iOS

  1. Hi i am new to ios and automation. kindly attach script file and provide more tutorial and examples.
    It’s a nice tutorial.

  2. Hi, I’m pretty new to automation with Instruments, and come to that, new to OS X as well. I want to use automation to create a defined set of screenshots from my app, which works pretty well by now. The only problem I have is that I can’t find the screenshots anywhere on my hard drive. I’ve already checked the “Continuously Log Results” option and selected a location to write the logs to, but in the target folder nothing appears at all.
    I’m also worried about the fact that the “Take Screenshot” button in the “Logging” section is grayed out.
    Maybe you have an idea what’s wrong?

  3. hi,
    how do I get the elements from a next screen?
    for example: I have a welcome screen containing two buttons Register and Sign In
    If I click on register, the application displays the Registration screen which contains 7 required fields and a submit button.
    How do I tell the script that another screen is opened so that there are another elements that should be retrieved ?
    I’ve also wrote some tests with Robotium for Android applications and I didn’t encountered this issue. Robotium automatically gets the elements from the current screen, so I’m stuck here

    • Hi Marius,

      Once the Registration View takes place you can add this command to your UITestScript.js.

      mainWindow.logElementTree();

      That will help you to get all the elements in the current view. And from there you can add a new test method(to your UITestScript.js) for the Registration view.

      Hope it helps.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>