77//
88
99// -----------------------------------------------------
10- // IMPORTANT: When modifying this file, make sure to
10+ // IMPORTANT: When modifying this file, make sure to
1111// increment the version number at the very
1212// bottom of the file to notify users about
1313// the new SnapshotHelper.swift
@@ -32,20 +32,48 @@ func snapshot(_ name: String, waitForLoadingIndicator: Bool = true) {
3232 Snapshot . snapshot ( name, waitForLoadingIndicator: waitForLoadingIndicator)
3333}
3434
35+ enum SnapshotError : Error , CustomDebugStringConvertible {
36+ case cannotDetectUser
37+ case cannotFindHomeDirectory
38+ case cannotFindSimulatorHomeDirectory
39+ case cannotAccessSimulatorHomeDirectory( String )
40+
41+ var debugDescription : String {
42+ switch self {
43+ case . cannotDetectUser:
44+ return " Couldn't find Snapshot configuration files - can't detect current user "
45+ case . cannotFindHomeDirectory:
46+ return " Couldn't find Snapshot configuration files - can't detect `Users` dir "
47+ case . cannotFindSimulatorHomeDirectory:
48+ return " Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable. "
49+ case . cannotAccessSimulatorHomeDirectory( let simulatorHostHome) :
50+ return " Can't prepare environment. Simulator home location is inaccessible. Does \( simulatorHostHome) exist? "
51+ }
52+ }
53+ }
54+
3555open class Snapshot : NSObject {
56+ static var app : XCUIApplication !
57+ static var cacheDirectory : URL !
58+ static var screenshotsDirectory : URL ? {
59+ return cacheDirectory. appendingPathComponent ( " screenshots " , isDirectory: true )
60+ }
3661
3762 open class func setupSnapshot( _ app: XCUIApplication ) {
38- setLanguage ( app)
39- setLocale ( app)
40- setLaunchArguments ( app)
63+ do {
64+ let cacheDir = try pathPrefix ( )
65+ Snapshot . cacheDirectory = cacheDir
66+ Snapshot . app = app
67+ setLanguage ( app)
68+ setLocale ( app)
69+ setLaunchArguments ( app)
70+ } catch let error {
71+ print ( error)
72+ }
4173 }
4274
4375 class func setLanguage( _ app: XCUIApplication ) {
44- guard let prefix = pathPrefix ( ) else {
45- return
46- }
47-
48- let path = prefix. appendingPathComponent ( " language.txt " )
76+ let path = cacheDirectory. appendingPathComponent ( " language.txt " )
4977
5078 do {
5179 let trimCharacterSet = CharacterSet . whitespacesAndNewlines
@@ -57,11 +85,7 @@ open class Snapshot: NSObject {
5785 }
5886
5987 class func setLocale( _ app: XCUIApplication ) {
60- guard let prefix = pathPrefix ( ) else {
61- return
62- }
63-
64- let path = prefix. appendingPathComponent ( " locale.txt " )
88+ let path = cacheDirectory. appendingPathComponent ( " locale.txt " )
6589
6690 do {
6791 let trimCharacterSet = CharacterSet . whitespacesAndNewlines
@@ -76,11 +100,7 @@ open class Snapshot: NSObject {
76100 }
77101
78102 class func setLaunchArguments( _ app: XCUIApplication ) {
79- guard let prefix = pathPrefix ( ) else {
80- return
81- }
82-
83- let path = prefix. appendingPathComponent ( " snapshot-launch_arguments.txt " )
103+ let path = cacheDirectory. appendingPathComponent ( " snapshot-launch_arguments.txt " )
84104 app. launchArguments += [ " -FASTLANE_SNAPSHOT " , " YES " , " -ui_testing " ]
85105
86106 do {
@@ -105,12 +125,18 @@ open class Snapshot: NSObject {
105125
106126 sleep ( 1 ) // Waiting for the animation to be finished (kind of)
107127
108- #if os(tvOS)
109- XCUIApplication ( ) . childrenMatchingType ( . Browser) . count
110- #elseif os(OSX)
128+ #if os(OSX)
111129 XCUIApplication ( ) . typeKey ( XCUIKeyboardKeySecondaryFn, modifierFlags: [ ] )
112130 #else
113- XCUIDevice . shared ( ) . orientation = . unknown
131+ let screenshot = app. windows. firstMatch. screenshot ( )
132+ guard let simulator = ProcessInfo ( ) . environment [ " SIMULATOR_DEVICE_NAME " ] , let screenshotsDir = screenshotsDirectory else { return }
133+ let path = screenshotsDir. appendingPathComponent ( " \( simulator) - \( name) .png " )
134+ do {
135+ try screenshot. pngRepresentation. write ( to: path)
136+ } catch let error {
137+ print ( " Problem writing screenshot: \( name) to \( path) " )
138+ print ( error)
139+ }
114140 #endif
115141 }
116142
@@ -127,30 +153,26 @@ open class Snapshot: NSObject {
127153 }
128154 }
129155
130- class func pathPrefix( ) -> URL ? {
156+ class func pathPrefix( ) throws -> URL ? {
131157 let homeDir : URL
132- //on OSX config is stored in /Users/<username>/Library
133- //and on iOS/tvOS/WatchOS it's in simulator's home dir
158+ // on OSX config is stored in /Users/<username>/Library
159+ // and on iOS/tvOS/WatchOS it's in simulator's home dir
134160 #if os(OSX)
135161 guard let user = ProcessInfo ( ) . environment [ " USER " ] else {
136- print ( " Couldn't find Snapshot configuration files - can't detect current user " )
137- return nil
162+ throw SnapshotError . cannotDetectUser
138163 }
139164
140165 guard let usersDir = FileManager . default. urls ( for: . userDirectory, in: . localDomainMask) . first else {
141- print ( " Couldn't find Snapshot configuration files - can't detect `Users` dir " )
142- return nil
166+ throw SnapshotError . cannotFindHomeDirectory
143167 }
144168
145169 homeDir = usersDir. appendingPathComponent ( user)
146170 #else
147171 guard let simulatorHostHome = ProcessInfo ( ) . environment [ " SIMULATOR_HOST_HOME " ] else {
148- print ( " Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable. " )
149- return nil
172+ throw SnapshotError . cannotFindSimulatorHomeDirectory
150173 }
151174 guard let homeDirUrl = URL ( string: simulatorHostHome) else {
152- print ( " Can't prepare environment. Simulator home location is inaccessible. Does \( simulatorHostHome) exist? " )
153- return nil
175+ throw SnapshotError . cannotAccessSimulatorHomeDirectory ( simulatorHostHome)
154176 }
155177 homeDir = URL ( fileURLWithPath: homeDirUrl. path)
156178 #endif
@@ -170,4 +192,4 @@ extension XCUIElement {
170192
171193// Please don't remove the lines below
172194// They are used to detect outdated configuration files
173- // SnapshotHelperVersion [1.4 ]
195+ // SnapshotHelperVersion [1.5 ]
0 commit comments