概要
SwiftでUITestを利用してスクリーンショットを撮るサンプルは見つけられたが、Objective-Cの例が見つからなかったので、自分用に残しておく。
コードを書かずに自動でスクリーンショットを撮る方法
Objective-C関係なく、Xcodeの設定のみで可能。
スキーマのTestの設定を開いて、チェックボックスを下記のようにする。
- UI TestingのCapture screenshots automaticallyにチェックを入れる
- 遷移した画面すべてを撮影してくれるようになる
- UI TestingのDelete when eath test succeedsのチェックを外す
- テスト成功時にスクリーンショットを削除しないようになる
- AttachmentsのDelete when eath test succeedsのチェックを外す
- テスト成功時にスクリーンショットを削除しないようになる
注意点としてものすごい量のスクリーンショットが作成されるので、必要のないもので必要なものが埋もれる可能性大。
管理が面倒臭い人は、任意の箇所で撮影する方法がいいかもしれない。
任意の箇所でスクリーンショットを撮る方法
2つの方法をあげているが、テスト完了後にレポートを見た際の見やすさが変わるだけなので、スクリーンショットを撮るコード自体は同じ。
用途に応じて選択すると吉。
シンプルにスクリーンショットを撮る方法
- (void)testTakeScreenShot { XCUIApplication *app = [[XCUIApplication alloc] init]; [app.buttons[@"ボタン"] tap]; XCUIScreen *screen = [XCUIScreen mainScreen]; // スクリーンショットを撮影 XCUIScreenshot *screenShot = [screen screenshot]; // アタッチメントを生成 XCTAttachment *attachment = [XCTAttachment attachmentWithScreenshot:screenShot]; // テスト実行後に削除しない attachment.lifetime = XCTAttachmentLifetimeKeepAlways; // スクリーンショットのファイル名のprefixに付加されるので設定しておくと楽 attachment.name = @"tapped-button"; [self addAttachment:attachment]; }
アクテビティを使ってログをグループ化する方法
- (void)testTakeScreenShot { XCUIApplication *app = [[XCUIApplication alloc] init]; [app.buttons[@"ボタン"] tap]; [XCTContext runActivityNamed:@"ボタンをタップ" block:^(id<XCTActivity> _Nonnull activity) { XCUIScreen *screen = [XCUIScreen mainScreen]; XCUIScreenshot *screenShot = [screen screenshot]; XCTAttachment *attachment = [XCTAttachment attachmentWithScreenshot:screenShot]; attachment.lifetime = XCTAttachmentLifetimeKeepAlways; attachment.name = @"tapped-button"; [activity addAttachment:attachment]; }]; }
撮影後のスクリーンショットの保存場所
下記ディレクトリ内に保存される。
/Users/(UserName)/Library/Developer/Xcode/DerivedData/(Project)/Logs/Test/Attachments/
参考
Xcode 9からテストが大幅に進化します! - Timers Tech Blog
Hands-on XCUITest Features with Xcode 9
iOS 11 Programming