Thursday, 12 April 2012

iOS - Integrating KIF Tests with Jenkins CI


Below is the script I use in Jenkins to kick off my automated GUI tests, which are performed using KIF which was developed by Square. KIF serves as an excellent start for running and automating GUI testing. I had to make a small change to KIF to support entering text into text fields which weren't empty; KIF didn't seem to handle this so I made a fix in a fork, located at my KIF.


One of the problems I had initially was that whilst the build would be marked as successful or a failure correctly, there was no feedback on which of the GUI tests failed. My script below solves this problem by outputting the feedback from KIF if and only if the GUI tests fail; if they are successful there is no need to see them in my opinion.


Your script would need to be adapted to contain the correct details for your project/workspace/scheme/target of course, but hopefully this will serve as a good guide.


Waxsim is the required bridging application needed to support integration between KIF and Jenkins. Waxsim has been installed on the server, running under /Applications/waxsim - if you have it installed elsewhere then update the reference accordingly.


#!/bin/sh
#kill simulator if running
killall -s "iPhone Simulator" &> /dev/null
if [ $? -eq 0 ]; then
    killall -KILL -m "iPhone Simulator"
fi


echo "About to build GUI tests scheme"


xcodebuild -scheme "GUITests" -workspace "xxx.xcworkspace" -configuration Debug -sdk "/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/" build CONFIGURATION_BUILD_DIR="/var/hudson/workspace/$JOB_NAME/build" 


echo "About to run waxsim - a long delay here might suggest something is obstructing the view of the simulator on build slave"


/Applications/waxsim "/var/hudson/workspace/$JOB_NAME/build/GUITests.app" > /tmp/KIF-$$.out 2>&1


# WaxSim hides the return value from the app, so to determine success we search for a "no failures" line


echo "Testing finished"


# count the number of times "TESTING FINISHED: 0 failures" is found - 0 means that there was a failure
success=`exec grep -c "TESTING FINISHED: 0 failures" /tmp/KIF-$$.out`


# if there was a failure, show what waxsim was hiding and crucially return with a non-zero exit code
if [ "$success" = '0' ]
then 
    cat /tmp/KIF-$$.out
    echo "==========================================="
    echo "GUI Tests failed"
    echo "==========================================="
    exit 1
else
    echo "==========================================="
    echo "GUI Tests passed"
    echo "==========================================="
fi

Tuesday, 10 April 2012

Script to Kill iPhone Simulator if Running

Instructions to end the iPhone Simulator process if it is running.

As part of a CI setup, it is typical to have multiple iOS build jobs running on an individual build slave. As you can only have a single instance of the iPhone Simulator, you might find some of your jobs fail because another job has started the simulator, and you can't get access to it.

What seems like a sensible approach here, is to kill the simulator if it is already running, meaning your build job is guaranteed to start from a known state. If you attempt to close the simulator when it isn't running, however, you will end up with an error, along the lines of "No matching processes belonging to you were found". That's why you need to check if the process is running first, before attempting to close it.

The following script checks if the simulator is running, and if it is, ends the process. You can do this at the start of each build job.


#!/bin/sh
#kill simulator if running
killall -s "iPhone Simulator" &> /dev/null
if [ $? -eq 0 ]; then
    killall -m -KILL "iPhone Simulator"
fi




And in case you were wondering; this works for the iPad simulator too. The process is the same so the script above works unedited.


If you have any problems getting this going, drop me a comment below.