@@ -18,8 +18,8 @@ import XCTest
18
18
var deviceLanguage = " "
19
19
var locale = " "
20
20
21
- func setupSnapshot( _ app: XCUIApplication ) {
22
- Snapshot . setupSnapshot ( app)
21
+ func setupSnapshot( _ app: XCUIApplication , waitForAnimations : Bool = true ) {
22
+ Snapshot . setupSnapshot ( app, waitForAnimations : waitForAnimations )
23
23
}
24
24
25
25
func snapshot( _ name: String , waitForLoadingIndicator: Bool ) {
@@ -63,14 +63,16 @@ enum SnapshotError: Error, CustomDebugStringConvertible {
63
63
@objcMembers
64
64
open class Snapshot : NSObject {
65
65
static var app : XCUIApplication ?
66
+ static var waitForAnimations = true
66
67
static var cacheDirectory : URL ?
67
68
static var screenshotsDirectory : URL ? {
68
69
return cacheDirectory? . appendingPathComponent ( " screenshots " , isDirectory: true )
69
70
}
70
71
71
- open class func setupSnapshot( _ app: XCUIApplication ) {
72
+ open class func setupSnapshot( _ app: XCUIApplication , waitForAnimations : Bool = true ) {
72
73
73
74
Snapshot . app = app
75
+ Snapshot . waitForAnimations = waitForAnimations
74
76
75
77
do {
76
78
let cacheDir = try pathPrefix ( )
@@ -114,10 +116,14 @@ open class Snapshot: NSObject {
114
116
} catch {
115
117
print ( " Couldn't detect/set locale... " )
116
118
}
117
- if locale. isEmpty {
119
+
120
+ if locale. isEmpty && !deviceLanguage. isEmpty {
118
121
locale = Locale ( identifier: deviceLanguage) . identifier
119
122
}
120
- app. launchArguments += [ " -AppleLocale " , " \" \( locale) \" " ]
123
+
124
+ if !locale. isEmpty {
125
+ app. launchArguments += [ " -AppleLocale " , " \" \( locale) \" " ]
126
+ }
121
127
}
122
128
123
129
class func setLaunchArguments( _ app: XCUIApplication ) {
@@ -149,22 +155,25 @@ open class Snapshot: NSObject {
149
155
150
156
print ( " snapshot: \( name) " ) // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work
151
157
152
- 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
+ }
153
161
154
162
#if os(OSX)
155
- XCUIApplication ( ) . typeKey ( XCUIKeyboardKeySecondaryFn, modifierFlags: [ ] )
156
- #else
157
-
158
163
guard let app = self . app else {
159
164
print ( " XCUIApplication is not set. Please call setupSnapshot(app) before snapshot(). " )
160
165
return
161
166
}
167
+
168
+ app. typeKey ( XCUIKeyboardKeySecondaryFn, modifierFlags: [ ] )
169
+ #else
162
170
163
- guard let window = app. windows . allElementsBoundByIndex . first ( where : { $0 . frame . isEmpty == false } ) else {
164
- print ( " Couldn't find an element window in XCUIApplication with a non-empty frame ." )
171
+ guard let app = self . app else {
172
+ print ( " XCUIApplication is not set. Please call setupSnapshot(app) before snapshot() ." )
165
173
return
166
174
}
167
-
175
+
176
+ let window = app. windows. firstMatch
168
177
let screenshot = window. screenshot ( )
169
178
guard let simulator = ProcessInfo ( ) . environment [ " SIMULATOR_DEVICE_NAME " ] , let screenshotsDir = screenshotsDirectory else { return }
170
179
let path = screenshotsDir. appendingPathComponent ( " \( simulator) - \( name) .png " )
@@ -182,7 +191,12 @@ open class Snapshot: NSObject {
182
191
return
183
192
#endif
184
193
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
186
200
let networkLoadingIndicatorDisappeared = XCTNSPredicateExpectation ( predicate: NSPredicate ( format: " exists == false " ) , object: networkLoadingIndicator)
187
201
_ = XCTWaiter . wait ( for: [ networkLoadingIndicatorDisappeared] , timeout: timeout)
188
202
}
@@ -257,7 +271,11 @@ private extension XCUIElementQuery {
257
271
}
258
272
259
273
var deviceStatusBars : XCUIElementQuery {
260
- let deviceWidth = XCUIApplication ( ) . 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
261
279
262
280
let isStatusBar = NSPredicate { ( evaluatedObject, _) in
263
281
guard let element = evaluatedObject as? XCUIElementAttributes else { return false }
@@ -277,4 +295,4 @@ private extension CGFloat {
277
295
278
296
// Please don't remove the lines below
279
297
// They are used to detect outdated configuration files
280
- // SnapshotHelperVersion [1.10 ]
298
+ // SnapshotHelperVersion [1.15 ]
0 commit comments