如何在ui测试时查看应用程序日志记录

Lyn*_*son 8 ui-testing xctest xcode-ui-testing

在我的Jenkins CI slave上的iOS应用程序上运行UI测试时,UI不正确,我的测试失败.在本地,相同的UI是预期的,测试通过.

我无法解决这个问题,我无法在远程计算机上进行调试.因此,我想添加日志记录以查看应用程序正在做出哪些决定,以找出UI在远程Jenkins从站中错误显示的原因.

测试日志输出不包含来自应用程序(通过NSLog)的日志输出,也无法NSLog在派生数据输出文件夹中的任何.xcactivitylog文件中找到应用程序调用中的任何字符串.

当UI测试运行时,我在哪里可以找到我的应用程序的日志输出?这是使用运行iOS 9.3模拟设备的Xcode 8.2.

Tit*_*eul 5

以下解决方案有点笨拙,但即使在设备上运行时也允许您获取日志。

记录器.h

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

void _Debug(NSString *format, ...);
Run Code Online (Sandbox Code Playgroud)

记录器

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);
}
Run Code Online (Sandbox Code Playgroud)

然后从 UI 测试,我可以访问这样的日志

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