在 xCode UI 测试中访问 UIKit 对象

Access to UIKit objects in xCode UI testing

我在UI测试的时候需要截图,但是我获取不到UIWindow对象,只有XCUIElement。有没有办法得到真正的 UIKit 对象? P.S。我正在使用 Objective-C.

在 UI 测试下无法访问底层 UIKit 对象,因为该框架旨在用作 black-box。测试基本上不知道引擎盖下发生了什么"really";他们只能通过可访问性 API.

进行交互

至于截图,你看过Xcode的测试输出了吗?点击"Show Report Navigator" ⌘8查看测试失败时Xcode自动截取的截图。

这些截图来自WWDC 2015 video on UI Testing

您还可以通过在特定时间明确测试失败来触发手动屏幕截图。确保关闭 continuesAfterFailure.

@interface ScreenshotTests : XCTestCase
@property (nonatomic) XCUIApplication *app;
@end

@implementation ScreenshotTests

- (void)setUp {
    [super setUp];

    self.continueAfterFailure = YES;
    self.app = [[XCUIApplication alloc] init];
    [self.app launch];
}

- (void)testManualScreenshot {
    [self.app.textFields[@"username"] typeText:@"joemasilotti"];
    [self recordFailureWithDescription:@"Taking Screenshot" inFile:@"" atLine:0 expected:YES];
}

@end