// // SnapshotHelper.swift // Example // // Created by Felix Krause on 10/8/15. // Copyright © 2015 Felix Krause. All rights reserved. // // This file should be used if your UI Tests are written in Swift 2.3 import Foundation import XCTest var deviceLanguage = "" var locale = "" @available(*, deprecated, message="use setupSnapshot: instead") func setLanguage(app: XCUIApplication) { setupSnapshot(app) } func setupSnapshot(app: XCUIApplication) { Snapshot.setupSnapshot(app) } func snapshot(name: String, waitForLoadingIndicator: Bool = true) { Snapshot.snapshot(name, waitForLoadingIndicator: waitForLoadingIndicator) } public class Snapshot: NSObject { public class func setupSnapshot(app: XCUIApplication) { setLanguage(app) setLocale(app) setLaunchArguments(app) } class func setLanguage(app: XCUIApplication) { guard let prefix = pathPrefix() else { return } let path = prefix.stringByAppendingPathComponent("language.txt") do { let trimCharacterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet() deviceLanguage = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding).stringByTrimmingCharactersInSet(trimCharacterSet) as String app.launchArguments += ["-AppleLanguages", "(\(deviceLanguage))"] } catch { print("Couldn't detect/set language...") } } class func setLocale(app: XCUIApplication) { guard let prefix = pathPrefix() else { return } let path = prefix.stringByAppendingPathComponent("locale.txt") do { let trimCharacterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet() locale = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding).stringByTrimmingCharactersInSet(trimCharacterSet) as String } catch { print("Couldn't detect/set locale...") } if locale.isEmpty { locale = NSLocale(localeIdentifier: deviceLanguage).localeIdentifier } app.launchArguments += ["-AppleLocale", "\"\(locale)\""] } class func setLaunchArguments(app: XCUIApplication) { guard let prefix = pathPrefix() else { return } let path = prefix.stringByAppendingPathComponent("snapshot-launch_arguments.txt") app.launchArguments += ["-FASTLANE_SNAPSHOT", "YES", "-ui_testing"] do { let launchArguments = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) as String let regex = try NSRegularExpression(pattern: "(\\\".+?\\\"|\\S+)", options: []) let matches = regex.matchesInString(launchArguments, options: [], range: NSRange(location:0, length:launchArguments.characters.count)) let results = matches.map { result -> String in (launchArguments as NSString).substringWithRange(result.range) } app.launchArguments += results } catch { print("Couldn't detect/set launch_arguments...") } } public class func snapshot(name: String, waitForLoadingIndicator: Bool = true) { if waitForLoadingIndicator { waitForLoadingIndicatorToDisappear() } print("snapshot: \(name)") // more information about this, check out https://github.com/fastlane/fastlane/tree/master/snapshot#how-does-it-work 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.sharedDevice().orientation = .Unknown #endif } class func waitForLoadingIndicatorToDisappear() { #if os(tvOS) return #endif let query = XCUIApplication().statusBars.childrenMatchingType(.Other).elementBoundByIndex(1).childrenMatchingType(.Other) while (0.. NSString? { var homeDir: NSString //on OSX config is stored in /Users//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 } guard let usersDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.NSUserDirectory, NSSearchPathDomainMask.NSLocalDomainMask, true)[0] as NSString? else { print("Couldn't find Snapshot configuration files - can't detect `Users` dir") return nil } homeDir = usersDir.stringByAppendingPathComponent(user) as NSString #else guard homeDir = ProcessInfo().environment["SIMULATOR_HOST_HOME"] as NSString else { print("Couldn't find simulator home location. Please, check SIMULATOR_HOST_HOME env variable.") return nil } #endif return homeDir.stringByAppendingPathComponent("Library/Caches/tools.fastlane") as NSString } } extension XCUIElement { var isLoadingIndicator: Bool { let whiteListedLoaders = ["GeofenceLocationTrackingOn", "StandardLocationTrackingOn"] if whiteListedLoaders.contains(self.identifier) { return false } return self.frame.size == CGSize(width: 10, height: 20) } } // Please don't remove the lines below // They are used to detect outdated configuration files // SnapshotHelperVersion [1.3]