How to view app logging when ui testing

4k views Asked by At

When running UI tests on my iOS app on our Jenkins CI slave, the UI is incorrect and my tests fail. Locally, the same UI is as expected and the tests pass.

I haven't been able to figure this out and I cannot debug it as it is on a remote machine. So, I would like to add logging to see what decisions are being made by the app to figure out why the UI is displaying incorrectly in the remote Jenkins slave.

The test log output does not contain log output from the app (via NSLog), nor can I find any of the strings from the appNSLog calls in any .xcactivitylog file in the derived data output folders.

Where can I find my logging output from my app when it is being run by the UI tests? This is with Xcode 8.2 running a iOS 9.3 simulated device.

1

There are 1 answers

1
Titouan de Bailleul On

The following solution is a bit hacky but allows you to get the logs even when running on the device.

Logger.h

#define Debug(...) _Debug(__VA_ARGS__)

void _Debug(NSString *format, ...);

Logger.m

void _Debug(NSString *format, ...)
{    
    format = [NSString stringWithFormat:@"[Debug]: %@", format];
    va_list args;
    va_start(args, format);
    NSLogv(format, args); // logs to the console

    NSString *logMessage = [[NSString alloc] initWithFormat:format arguments:arguments];
    UIApplication *application = [UIApplication sharedApplication];
    UIWindow *window = [application keyWindow];
    window.accessibilityValue = [NSString stringWithFormat:@"%@\n%@", window.accessibilityValue, logMessage];

    va_end(args);
}

Then from the UI Test, I can access the logs like this

+ (NSString *)logs
{
    XCUIElementQuery *windowQuery = [[[XCUIApplication alloc] init] windows];
    return [windowQuery elementAtIndex:0].value;
}