Getting Started with KIF for Functional iOS UI Testing

  December 27, 2016

Since the deprecation of UI Automation from the latest Xcode, there has been lots of buzz around XCTest and XCUITest. These frameworks have served many of you since Xcode 5 and many of you have used frameworks that are built on top of XCTest implementation. KIF – Keep It Functional – is a great example of those frameworks.

In this blog, we’ll take a look at what KIF has to offer for UI test automation and how to get started with it.

Bitbar Public Cloud Product Update

With the December release (22nd of December 2016), KIF support was enabled for Bitbar Public Cloud users. With the support of XCTest project type in Public Cloud, it’s now possible to also execute KIF tests in Public Cloud and the same support has been available for Private Cloud and On-Premise solutions for some time already.

There are questions every now and then why should you use KIF as there are plenty of other frameworks available and nevertheless it is tightly integrated with XCTest. Are there any benefits of using KIF over the regular XCTest for iOS app testing? We’ll take a look at it today.

Download This Guide to Learn Everything about KIF/XCTest

Introduction to KIF iOS Integration Testing Framework

KIF (short for “keep it functional”) is an iOS integration test framework that is closely related to and that uses XCTest test targets. KIF tests can be executed directly in XCTestCase or any subclass. KIF allows for easy automation of iOS applications by leveraging the accessibility attributes that the OS makes available to those with visual disabilities.

One of those things that why KIF has got some popularity among iOS developers is that it performs those tests using standard Xcode test targets and all tests can be written using Objective-C or Swift as normal XCTest subclasses.

Features and Benefits of KIF

Easy to Learn – No Real Learning Curve
As Swift and Objective-C are the options for iOS app developers to use, KIF is very easy to use in Swift and Objective-C based projects and it supports apps written either with Swift or Objective-C. This basically limits the complexity that typically comes from the test setups where several languages are used.

KIF integrates directly to Xcode project and there is no need to install any packages, set up additional servers, or integrate with any other instances.

Support for Various iOS Versions
Since XCTest was introduced in Xcode, iOS has seen several major (and minor) releases after that. However, XCTest has remained consistent test option for iOS app developers and OS version fragmentation hasn’t had too much impact on how it can be used for test automation. Basically KIF supports even older iOS versions and from the user point of view changes required to get things running are minimalistic (all related to Xcode).

User-like Test Automation
KIF among the other frameworks provides user-like inputs and all events and interactions for automation are similar to how an actual user would use the app under test. In addition, XCTest/XCUITest foundation provides great API to a variety of different user interactions, clicks and even gestures. The basic selection of gesture and interaction API includes:

func tap()
func doubleTap()
func twoFingerTap()
func tap(withNumberOfTaps: UInt, numberOfTouches: UInt)
func press(forDuration: TimeInterval)
func press(forDuration: TimeInterval, thenDragTo: XCUIElement)
func swipeLeft()
func swipeRight()
func swipeUp()
func swipeDown()
func pinch(withScale: CGFloat, velocity: CGFloat)
func rotate(CGFloat, withVelocity: CGFloat)

More information about XCTest elements can be found under Apple’s documentation.

Development Tool Compatibility
The only tool iOS developer needs to design, develop and test their iOS apps is the Xcode environment. KIF and XCTest are tightly integrated with it and for example Test Navigator, Bots and other Xcode features are readily available and handy to use with KIF tests.

How to Get Started with KIF

First of all, download the KIF source assets from Github and place it somewhere where you can easily find it. Alternatively, you can use Git’s submodule to get the source code for your local use:

$ git init
$ git submodule add Folder/Here

NOTE! This needs to be added to your iOS project folder (=Folder/Here should be under your iOS project).

In order to install KIF, you’ll need to link the libKIF static library directly to your app. Also, there is a readily available example for Swift under the KIF repository.

Working on Xcode

To use KIF with your iOS app you have now placed its project, source and test templates under your iOS project. You can also open KIF.xcodeproj in Project Navigator and inspect the following files included with the installation:

kif files from installation

Now, click MainStoryboard.storyboard open (under Test Host folder) and the following scene should be open and editable:

mainstoryboard in xcode

I’ll come back with a more detailed article about storyboard use and how to get the best out of it, but for this one, it’s good advice to take a look what is included under “Master Scene” and “Navigation Controller Scene”. This should give you basic info on how to work with storyboard features in Xcode.

Adding a new Testing Target

Depending on which Xcode you are using (and yes, we’re encouraging people to use the latest and greatest) creating a new test target can be done via different routes.

testing target in xcode

Under File -> New -> Target you can find the testing target wizard:

uitesting and unit testing in xcode

Configure the parameters and create a testing target (click Finish):

creating a new testing target in xcode

Now, you can configure your iOS project. First, click your project name on Project Navigator and you will see the “Build Phases” on the right side. Open “Link Binary With Libraries” and locate libKIF.a and click Add. KIF will be now added to your project and library is linked as a binary with your app.

libKIF.a configuration

Finally, KIF requires IOKit so this can be added to your project under “Build Settings” -> Linking and “Other Linker Flags”. Just add “-framework IOKit” there as a parameter and all should be good:

adding iokit framework in xcode

Now, that’s the basic information to get you started with KIF. More detailed instruction on how to use KIF efficiently will be posted later.

Happy iOS Testing Folks!