How to take screenshots for failing tests with KIF

How to take screenshots for failing tests with KIF

In the Consumer iOS app team we use KIF to test our application. KIF (Keep It Functional) is an open source tool for iOS automation from square. More details here: https://github.com/kif-framework/KIF
Taking screenshots for failing tests is a good way to understand why the test is failing.
Most test automation tools come with some way of taking screenshots. To enable taking screenshots for failing tests in KIF, we need to set an environment variable “KIF_SCREENSHOTS= ‘location’”. Location is the path which tells KIF where to save the screenshots.
To make sure that screenshots are saved for all failing tests, irrespective of the machine they run on, ‘location’ should be relative. I used the location ‘$PROJECT_DIR/Failed_Screenshots’.
$PROJECT_DIR is an Xcode build setting which stores the value of the Xcode project directory. This will already be set to where the project is on your system. To see the value of $PROJECT_DIR, go to the terminal, navigate to where the Xcode project is, and type ‘xcodebuild -showBuildSettings | grep PROJECT_DIR’.
KIF creates the Failed_Screenshots folder automatically, whenever a test fails and saves screenshots for all the failing tests.
Below are the steps to enable screenshots from Xcode:

How to enable screenshots using Xcode

  • Select the test scheme in Xcode

 

  • Select Edit Scheme

schemes

  • Select Run and set an environment variable KIF_SCREENSHOTS= ‘location_to_Save_screenshots’

variable settings

  • Select Close

 

  • Write a failing test

test file
test file2
This is a sample test which I created to demonstrate how KIF can take screenshots. This test is looking for an accessibility label which does not exist and always fails.

  • Run the test using Xcode or xcodebuild on command line

 

  • Check the screenshot in the failed screenshots folder, which you set as the value of the KIF_SCREENSHOTS variable. The screenshot is saved with the same name as the test class and also includes the line number of the failing step. For example, ‘JETESTScreenshots.m, line 17.png’, in this case ‘JETESTScreenshots.m’ is the test class and line 17 is the line number of the failing step.

 

  • Give the path to the ‘Failed_Screenshots’ folder in the artifact paths field of the project in your CI system, to save the screenshots as artifact for every failing build job.

 
Thanks for reading.
Preet

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