         }
     }
 
-    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


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

        #if os(tvOS)
            XCUIApplication().childrenMatchingType(.Browser).count
        #elseif os(OSX)
            XCUIApplication().typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: [])
        #else
            XCUIDevice.shared.orientation = .unknown
        #endif
    }


    Snapshot.snapshot(name, waitForLoadingIndicator: waitForLoadingIndicator)
}

enum SnapshotError: Error, CustomDebugStringConvertible {
    case cannotDetectUser
    case cannotFindHomeDirectory
    case cannotFindSimulatorHomeDirectory
    case cannotAccessSimulatorHomeDirectory(String)
    
    var debugDescription: String {
        switch self {
        case .cannotDetectUser:
            return "Couldn't find Snapshot configuration files - can't detect current user "
        case .cannotFindHomeDirectory:
            return "Couldn't find Snapshot configuration files - can't detect `Users` dir"
        case .cannotFindSimulatorHomeDirectory:
            return "Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable."
        case .cannotAccessSimulatorHomeDirectory(let simulatorHostHome):
            return "Can't prepare environment. Simulator home location is inaccessible. Does \(simulatorHostHome) exist?"
        }
    }
}

open class Snapshot: NSObject {
    static var app: XCUIApplication!
    static var cacheDirectory: URL!
    static var screenshotsDirectory: URL? {
        return cacheDirectory.appendingPathComponent("screenshots", isDirectory: true)
    }

    open class func setupSnapshot(_ app: XCUIApplication) {
        do {
            let cacheDir = try pathPrefix()
            Snapshot.cacheDirectory = cacheDir
            Snapshot.app = app
            setLanguage(app)
            setLocale(app)
            setLaunchArguments(app)
        } catch let error {
            print(error)
        }
    }

    class func setLanguage(_ app: XCUIApplication) {
        let path = cacheDirectory.appendingPathComponent("language.txt")

        do {
            let trimCharacterSet = CharacterSet.whitespacesAndNewlines
