Log-grabber: How to get iOS application logs while running XCUITest?

Amit Gupta
VMware 360
Published in
2 min readJan 6, 2020

XCUITest is a great tool to automate iOS application testing. Application UI tests run as a separate target with an additional proxy application installed on the device. Due to the iOS sandbox application architecture, it is hard for test code to get the access to the application container to retrieve the logs generated by the application.

So, what can we do to get the logs generated by the application? There could be two approaches

  1. Using a log Server: In this approach, one can write lightweight log server or use any existing commercial log server available on the cloud to get and post messages from the application. Test code can then grab the log messages using REST APIs.
  2. Using a UI Element: This approach is based on creating a UI Element in the application under a conditional compiler flag and dump all required log messages into the element. Subsequently, these log messages can be retrieved and validated using XCUITests.

There are certainly few advantages using a log server approach but I will discuss those in a follow-up post. In this article we are going to discuss about the second approach.

As we know, XCUITests can access the UI elements of the application under test. UI Elements act as a conduit between the test and the application. Now, by attaching a small (1x1 px) UI element in the application window, the application can start dumping all its logs statements and have it immediately accessible by the test process.

Steps to follow

In the application

  1. Add a UITextView element in the application’s UI Window so that UITextView is available across all screens of the application.
  2. Add a class which will provide methods to append log statements in this UI Element.
AutomationLoggingView
XCTestLogging in UITextView

In the XCUITests

In XCUITest, we can get the UIElement, get the logs information and parse the information we would like to validate.

  1. Create validation methods to verify logs.

2. Extend XCUIApplication class to use the validation methods

As we can see above, with very little code we can get application logs in XCUITests. You should declare all such code under a feature flag or #ifdef so logging is available only when required.

Why should we choose this approach?

Advantages:

  1. Easy fast reliable
  2. Works with simulator and real devices without any change in code or configuration.
  3. No external dependencies.
  4. No network connection required

Disadvantages:

  1. Platform dependent.
  2. Needs additional code in application.
  3. Logs will be lost if automation needs to kill the app.

Full sample code is available at Git Hub.

Lookout for my upcoming post on Log-Server based approach. We will explore how can we build a small log-server and run locally as part of automation and GET/POST log messages from XCUITests.

--

--

Amit Gupta
VMware 360

Entrepreneur, Mobile App Developer/Architect, Automation Architect, DevOps Engineer and Photographer.