Skip to content

Commit ddec373

Browse files
committed
Update the fastlane snapshot helper
1 parent a6cfaf6 commit ddec373

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

fastlane/SnapshotHelper.swift

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import XCTest
1818
var deviceLanguage = ""
1919
var locale = ""
2020

21-
func setupSnapshot(_ app: XCUIApplication) {
22-
Snapshot.setupSnapshot(app)
21+
func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) {
22+
Snapshot.setupSnapshot(app, waitForAnimations: waitForAnimations)
2323
}
2424

2525
func snapshot(_ name: String, waitForLoadingIndicator: Bool) {
@@ -63,14 +63,16 @@ enum SnapshotError: Error, CustomDebugStringConvertible {
6363
@objcMembers
6464
open class Snapshot: NSObject {
6565
static var app: XCUIApplication?
66+
static var waitForAnimations = true
6667
static var cacheDirectory: URL?
6768
static var screenshotsDirectory: URL? {
6869
return cacheDirectory?.appendingPathComponent("screenshots", isDirectory: true)
6970
}
7071

71-
open class func setupSnapshot(_ app: XCUIApplication) {
72+
open class func setupSnapshot(_ app: XCUIApplication, waitForAnimations: Bool = true) {
7273

7374
Snapshot.app = app
75+
Snapshot.waitForAnimations = waitForAnimations
7476

7577
do {
7678
let cacheDir = try pathPrefix()
@@ -115,7 +117,7 @@ open class Snapshot: NSObject {
115117
print("Couldn't detect/set locale...")
116118
}
117119

118-
if locale.isEmpty {
120+
if locale.isEmpty && !deviceLanguage.isEmpty {
119121
locale = Locale(identifier: deviceLanguage).identifier
120122
}
121123

@@ -153,10 +155,17 @@ open class Snapshot: NSObject {
153155

154156
print("snapshot: \(name)") // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work
155157

156-
sleep(1) // Waiting for the animation to be finished (kind of)
158+
if Snapshot.waitForAnimations {
159+
sleep(1) // Waiting for the animation to be finished (kind of)
160+
}
157161

158162
#if os(OSX)
159-
XCUIApplication().typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: [])
163+
guard let app = self.app else {
164+
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
165+
return
166+
}
167+
168+
app.typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: [])
160169
#else
161170

162171
guard let app = self.app else {
@@ -182,7 +191,12 @@ open class Snapshot: NSObject {
182191
return
183192
#endif
184193

185-
let networkLoadingIndicator = XCUIApplication().otherElements.deviceStatusBars.networkLoadingIndicators.element
194+
guard let app = self.app else {
195+
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
196+
return
197+
}
198+
199+
let networkLoadingIndicator = app.otherElements.deviceStatusBars.networkLoadingIndicators.element
186200
let networkLoadingIndicatorDisappeared = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == false"), object: networkLoadingIndicator)
187201
_ = XCTWaiter.wait(for: [networkLoadingIndicatorDisappeared], timeout: timeout)
188202
}
@@ -257,7 +271,11 @@ private extension XCUIElementQuery {
257271
}
258272

259273
var deviceStatusBars: XCUIElementQuery {
260-
let deviceWidth = XCUIApplication().windows.firstMatch.frame.width
274+
guard let app = Snapshot.app else {
275+
fatalError("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
276+
}
277+
278+
let deviceWidth = app.windows.firstMatch.frame.width
261279

262280
let isStatusBar = NSPredicate { (evaluatedObject, _) in
263281
guard let element = evaluatedObject as? XCUIElementAttributes else { return false }
@@ -277,4 +295,4 @@ private extension CGFloat {
277295

278296
// Please don't remove the lines below
279297
// They are used to detect outdated configuration files
280-
// SnapshotHelperVersion [1.13]
298+
// SnapshotHelperVersion [1.15]

0 commit comments

Comments
 (0)