やること
FastlaneのSnapshotを使ってUITestでスクリーンショットをとります。
環境
Fastlane 2.68.2
Xcode 9.1
Swift 4
手順
Fastlaneを導入
参考: https://qiita.com/SatoTakeshiX/items/fea8d49216a2a1086cf9
$ bundle init
$ vim Gemfile
# frozen_string_literal: true
source "https://rubygems.org"
# gem "rails"
gem "fastlane"
gem "cocoapods"
$ bundle install --path vendor/bundler
UITestのTarget準備
参考: https://qiita.com/taji-taji/items/c00e5b94376c37f17443
「File」→「New」→「Target」→「iOS」→「Test」→「iOS UI Testing Bundle」を選択。
Snapshot準備
※下記のサンプルではいろいろ怒られてますが、それでも./fastlane/Snapfile
を作成してくれます。
$ fastlane snapshot init
[15:55:57]: fastlane detected a Gemfile in the current directory
[15:55:57]: however it seems like you don't use `bundle exec`
[15:55:57]: to launch fastlane faster, please use
[15:55:57]:
[15:55:57]: $ bundle exec fastlane snapshot init
[15:55:57]:
[15:55:57]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
✅ Successfully created SnapshotHelper.swift './fastlane/SnapshotHelper.swift'
✅ Successfully created new Snapfile at './fastlane/Snapfile'
-------------------------------------------------------
Open your Xcode project and make sure to do the following:
1) Add a new UI Test target to your project
2) Add the ./fastlane/SnapshotHelper.swift to your UI Test target
You can move the file anywhere you want
3) Call `setupSnapshot(app)` when launching your app
let app = XCUIApplication()
setupSnapshot(app)
app.launch()
4) Add `snapshot("0Launch")` to wherever you want to create the screenshots
1), 2), 3), 4)を参考に設定を進めていきます。
1) Targetは上で追加していれば怒られないはず。
2) ./fastlane/SnapshotHelper.swift
にファイルができているので、それを先ほど作成したUITestのTargetに追加します。(Command + Option + A)
3) UITestのファイルのsetUpを下記みたいな感じに修正
override func setUp() {
super.setUp()
continueAfterFailure = false
let app = XCUIApplication()
setupSnapshot(app)
app.launch()
}
4) UITestのファイルのtestExampleを下記みたいな感じに修正
func testExample() {
snapshot("0Launch")
}
Snapfile
Snapfileを以下を参考に修正
# 適当な端末を選択。多いとテストが遅くなるので最初は1つでいいです
devices([
"iPhone 7",
"iPhone 7 Plus",
"iPhone X",
"iPhone SE",
])
# 日本語。他の言語でテストしたい場合は追加してください
languages([
"ja-JP",
])
# テスト対象のスキーマを入力
scheme "Scheme"
# screenshotの出力先
output_directory "./fastlane/screenshots"
# 毎回screentshotディレクトリを空にするかどうか
clear_previous_screenshots true
実際に動かしてみる
注意事項
・実機ビルドだと落ちます
・XcodeのTestで実行してもスクリーンショットは取れません
$ fastlane snapshot run
これで./fastlane/screenshots
にスクリーンショットが保存されていればOK!
おまけ
【Xcode9.1】iOSのUITestとかでアプリを削除したりデータをResetしたりする方法 - Qiita
https://qiita.com/rd0501/items/8e822c1830ad1bb2a7c0