Recording Your XCTests on CircleCI
CircleCI is a great platform for you to run your XCode tests with ease. You can follow these documentations and implement continuous integration to your iOS project easily. CircleCI can run tests and provides built-in support for you to see your test results as HTML. However, wouldn’t it be better to see the video recordings of your tests?
There isn’t support for recording videos but luckily CircleCI uses a macOS environment and has QuickTime pre-installed. We can use AppleScript to record videos via QuickTime.
You can find an example configuration in this repository if you are already familiar with CircleCI.
WRITING VIDEO RECORDING SCRIPTS
Let’s start with writing our scripts. We will use AppleScript so we need to open Script Editor. AppleScript is a scripting language which can automate jobs on MacOS computers. You can find detailed documentation here. In order to record our tests, we need to tell our computer to open QuickTime and start recording. For this, we can use the below code.
This script, when executed, tells QuickTime Player to start a new screen recording. There is a 3 seconds delay after the recording starts. Script editor saves those files in ‘.scpt’ format. (We can use the name ‘start_recording.scpt’ for this one.)
We also need to tell the computer to stop recording when the tests finish. The below code says QuickTime to stop recording and save it as ‘testrecording.mov’ in 480p format. (Let’s save it as ‘stop_recording.scpt’)
CONFIGURING FASTLANE
Fastlane is an open-source platform developed on top of Ruby which simplifies Android and iOS testing and deployment. It can be easily used with CircleCI, you can find the whole documentation here.
Fastlane uses a special file called Fastfile and we will configure it to do the following:
Start Recording -> Run XCTests -> Stop Recording
We can execute the osascript command from the terminal in order to run our applescripts and Fastlane’s commands in order to run our test.
In order to open the recordings from a web browser, we can also format it to .mp4 using ffmpeg.
CONFIGURING CIRCLECI
As these scripts are ready it is time to call them from our configuration file. (You can use only CircleCI’s config.yml file for this but using it along Fastlane is far more comprehensive.)
Our config should start the necessary lane with every commit as shown below.
With every commit, this config file starts the build_test job which starts the simulator, updates Fastlane and starts the build_and_test lane which will start the recording, execute the tests then stop the recording.
We will also use CircleCI’s artifact feature in order to save our test results and video recordings.
CONCLUSION
By using these configurations, when a commit goes to Github, CircleCI config will run and call Fastlane after starting the simulator and updating Fastlane. Then, Fastlane will start screen recording using our Apple Scripts and run the tests. After the XC Tests are completed, Fastlane will stop the recording and CircleCI will save the recording as artifacts.