         }
     }
 
-    class func pathPrefix() -> URL? {
+    class func pathPrefix() throws -> URL? {
         let homeDir: URL
-        //on OSX config is stored in /Users/<username>/Library
-        //and on iOS/tvOS/WatchOS it's in simulator's home dir
+        // on OSX config is stored in /Users/<username>/Library
+        // and on iOS/tvOS/WatchOS it's in simulator's home dir
         #if os(OSX)
             guard let user = ProcessInfo().environment["USER"] else {
-                print("Couldn't find Snapshot configuration files - can't detect current user ")
-                return nil
+                throw SnapshotError.cannotDetectUser
             }
 
             guard let usersDir =  FileManager.default.urls(for: .userDirectory, in: .localDomainMask).first else {
-                print("Couldn't find Snapshot configuration files - can't detect `Users` dir")
-                return nil
+                throw SnapshotError.cannotFindHomeDirectory
             }
 
             homeDir = usersDir.appendingPathComponent(user)
         #else
             guard let simulatorHostHome = ProcessInfo().environment["SIMULATOR_HOST_HOME"] else {
-                print("Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable.")
-                return nil
+                throw SnapshotError.cannotFindSimulatorHomeDirectory
             }
             guard let homeDirUrl = URL(string: simulatorHostHome) else {
-                print("Can't prepare environment. Simulator home location is inaccessible. Does \(simulatorHostHome) exist?")
-                return nil
+                throw SnapshotError.cannotAccessSimulatorHomeDirectory(simulatorHostHome)
             }
             homeDir = URL(fileURLWithPath: homeDirUrl.path)
         #endif

    }

    class func setLaunchArguments(_ app: XCUIApplication) {
        guard let prefix = pathPrefix() else {
            return
        }

        let path = prefix.appendingPathComponent("snapshot-launch_arguments.txt")
        app.launchArguments += ["-FASTLANE_SNAPSHOT", "YES", "-ui_testing"]

        do {


        sleep(1) // Waiting for the animation to be finished (kind of)

        #if os(OSX)
            XCUIApplication().typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: [])
        #else
            let screenshot = app.windows.firstMatch.screenshot()
            guard let simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return }
            let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png")
            do {
                try screenshot.pngRepresentation.write(to: path)
            } catch let error {
                print("Problem writing screenshot: \(name) to \(path)")
                print(error)
            }
        #endif
    }

