Thursday 11 April 2013

iOS UI Automation






Introduction / Overview

iPhone apps are developed using native or native-hybrid bridge.

native-hybrid : The datas are rendered in HTML and the native actions are performed using js callbacks

Automation testing framework for these applications is mainly to,

  • Reduce the manual testing effort

  • Reduce the testing cycles involved for any changes to the application

  • Provide maximum test coverage


images



Why UIAutomation ?


Selenium Automation

Using selenium the basic level functionalities like tapping, swiping could not be done in the iPhone app. so,selenium could not be an option to automate the iPhone app
Jasmine framework


  •     Jasmine is a BDD (Behavior Driven Development) framework.

  •     Jasmine is mainly used for Unit testing

  •     More or less jasmine is same as tuneup framework

  •    Jasmine provides good reporting structure

  •    Reports can be in xml or html format


UI Automation + tuneup.js


  •     It is the Apple's inbuilt Automation testing framework.

  •     It is highly stable and also reliable testing framework.

  •     It uses javascript to automate the testcases.

  •     Automation can also be performed in the specified devices

  •     It can also be accessible from command line so that we can also integrate this with Hudson

  •     Considering its salient features,simplicity UIAutomation is preferred for automating the test cases over other testing frameworks


Tools used



  •      Xcode IDE (Version 4.X and above)

  •      Instruments


Instruments Tool

Instruments is a tool for UIAutomation provided by Xcode. Instruments provides us the record and playback feature which will be very useful in doing automation

It can be used to collect data about the performance and behavior of one or more processes on the system and track that data over time.
Test Case Log

The test case when executed on the iOS application target, using Instruments,

The Instruments console will show the details of,

Log Message - What Test case is being executed

Log Type - Test case is Pass or Fail.

If a test case fails, it would display the

  •  Element tree in the failed page meaning, what are the UI elements present in the current page.

  •   Also the screen shot of the page in which error occured.


Automation Framework


Tune-up  is the framework used for automation

Tune-up is a collection of JavaScript utilities that builds upon and improves the UIAutomation library provided by Apple for testing iOS applications via Instruments.
You can find more details about the Tune-up framework from here:https://github.com/alexvollmer/tuneup_js/blob/master/README.md
an also in http://alexvollmer.com/posts/2010/07/03/working-with-uiautomation/

Test Structure in Tune-up


To create a test case, use the test() function, which takes the name of the test case as a string, and a function. The function will be given a UIATarget instance and a UIAApplication instance.

For example:


test("Sign In to the app", function(target, app) {

// The target and app arguments are root elements

/* (i.e) The element structure of every element in the app goes in this way
UIATarget.localTarget().frontMostApp().(position of the particular element in the view)
for reusing UIATarget.localTarget().frontMostApp() we store it in target,app   */

// The UIATarget is the primary portal into the application running on the device or simulator

// Steps for Sign-in

});

test("Sign out from the app", function(target, app) {

// Steps for Sign-out

});





Integrating with the Project



MakeFile configuration:


Configure the MakeFile which is in the project home , with the command lines which is used to build the project and Launch the instrument.

Add the following configuration,

1.TRACE_PATH : Location to store the .trace file, which will create after the execution of test script using instruments.

2.TEMPLATE_PATH : Location where the test case TemplateTrace files are stored, which is created from the instruments by saving the testSuite script as Template.

3.uiautomation :

Shell command which

  • Builds the project ,

  • Executes the test case script by launching the Instruments tool.

  • And this executes the test Suite which has the entire flow.


Ex.
/* Specify the Trace path, Template path and App path */












TRACE_PATH = TraceDocument/UIAutomationTrace

TEMPLATE_PATH = Test/Automation/Resources/TestCaseTemplates

APP_PATH = /Users/${projectpath}//Products/Debug-iphonesimulator/sample.app





/* Shell commands to Run the Test cases */












uiautomation:


    xcodebuild -workspace sample.xcworkspace -scheme Release -sdk iphonesimulator5.0


    instruments -t ${TEMPLATE_PATH}/testSuiteTemplate.tracetemplate -D ${TRACE_PATH} ${APP_PATH}





Running the Automation Script


Command to run automation from Terminal using the MakeFile ,

In Terminal ,Move to the directory where the ‘.xcodeproj‘ file resides

And execute the following Command,











make uiautomation





Additional Info about Shell Commands


Write a Shell Script to execute the following commands

1) Building the Project


At first the Project should be build through ‘xcodebuild’ command

xcodebuild:

This command is used to build the xcode project.

Xcodebuild Usage

Xcodebuild -- workspace [PROJECT-WORKSPACE] --scheme [SCHEME-NAME] --sdk [DEVICE/SIMULATOR NAME]




















Option Description
-- workspacePROJECT-WORKSPACE
Name of the Project file(ex sample.xcworkspace)
--schemeSCHEME-NAME
Name of the Sheme
--sdkSpecify the device/simulator name(ex: simulator name – iPhoneSimulator5.0)


Move to the directory where the ‘.xcodeproj‘ file resides and then the command Should be executed

Ex:

Move to the directory where the project resides

Execute the xcodebuild command

$bash: xcodebuild -workspace sample.xcworkspace -scheme Release -sdk iphonesimulator5.0

This command will build the project with configuration as release and sdk as iphone simulator5.0

2) Launching the Instruments tool


Execute a command to launch the Instrument with the latest build

This command executes the saved ‘.tracetemplate’ file and saves the result in the path specified
Instruments Command Usage

instruments [-t template] [-d document] [-w device] [-p pid] | [application [-e variable value ]]




































Options                                                 Description
-t     templateFile name of type ‘ .tracetemplate ‘ with the path of  the file location
-s     Show list of known templates and exit
-d     documentThe path to save the trace document data to (This may already exist, in which case
a new Run will be added)
-p     pidThe ID of the process to attach to
application     The path to the application or command to launch
-w     hardware deviceThe identifier of the hardware to target
-e     variable valueAn environment variable setting (You may specify more than one)


Ex :

       instruments -t $TEMPLATE_PATH -D $TRACE_PATH  $APP_PATH
























OptionDescriptionExample
TEMPLATE_PATHTemplate path is the path of the saved template file$PROJECT_HOME/Test/Automation/Resources/TestCaseTemplates/testSuite.tracetemplate
TRACE_PATHIt is the path where the .trace will be saved after executing the automation$PROJECT_HOME/TraceDocument/UIAutomationTrace
APP_PATHIt is the path of the latest build of the APP~/Library/Developer/Xcode/DerivedData/sample-gxfepasnjxtmhjcevjkpyunezjca/Build/Products/Debug-iphonesimulator/sample.app


Note: After the execution of the xcodebuild command the app will be build in a directory; APP_PATH should have that directory path

The Build path can be customized. To change the app build path, In  the Xcode Choose the Xcode option int the Menu bar choose Preferences – Locations and then give the custom location in the derived data field

On executing this command, the script loaded in the .tracetemplate file will get executed on the specified simulator/device and stores the .trace file in the specified path



1 comment: