Skip to content

Commit

Permalink
Updated SnapshotHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
weiran committed Aug 18, 2019
1 parent 8322869 commit f3da344
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions fastlane/SnapshotHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ open class Snapshot: NSObject {
setLocale(app)
setLaunchArguments(app)
} catch let error {
print(error)
NSLog(error.localizedDescription)
}
}

class func setLanguage(_ app: XCUIApplication) {
guard let cacheDirectory = self.cacheDirectory else {
print("CacheDirectory is not set - probably running on a physical device?")
NSLog("CacheDirectory is not set - probably running on a physical device?")
return
}

Expand All @@ -98,13 +98,13 @@ open class Snapshot: NSObject {
deviceLanguage = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet)
app.launchArguments += ["-AppleLanguages", "(\(deviceLanguage))"]
} catch {
print("Couldn't detect/set language...")
NSLog("Couldn't detect/set language...")
}
}

class func setLocale(_ app: XCUIApplication) {
guard let cacheDirectory = self.cacheDirectory else {
print("CacheDirectory is not set - probably running on a physical device?")
NSLog("CacheDirectory is not set - probably running on a physical device?")
return
}

Expand All @@ -114,7 +114,7 @@ open class Snapshot: NSObject {
let trimCharacterSet = CharacterSet.whitespacesAndNewlines
locale = try String(contentsOf: path, encoding: .utf8).trimmingCharacters(in: trimCharacterSet)
} catch {
print("Couldn't detect/set locale...")
NSLog("Couldn't detect/set locale...")
}

if locale.isEmpty && !deviceLanguage.isEmpty {
Expand All @@ -128,7 +128,7 @@ open class Snapshot: NSObject {

class func setLaunchArguments(_ app: XCUIApplication) {
guard let cacheDirectory = self.cacheDirectory else {
print("CacheDirectory is not set - probably running on a physical device?")
NSLog("CacheDirectory is not set - probably running on a physical device?")
return
}

Expand All @@ -144,7 +144,7 @@ open class Snapshot: NSObject {
}
app.launchArguments += results
} catch {
print("Couldn't detect/set launch_arguments...")
NSLog("Couldn't detect/set launch_arguments...")
}
}

Expand All @@ -153,35 +153,40 @@ open class Snapshot: NSObject {
waitForLoadingIndicatorToDisappear(within: timeout)
}

print("snapshot: \(name)") // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work
NSLog("snapshot: \(name)") // more information about this, check out https://docs.fastlane.tools/actions/snapshot/#how-does-it-work

if Snapshot.waitForAnimations {
sleep(1) // Waiting for the animation to be finished (kind of)
}

#if os(OSX)
guard let app = self.app else {
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

app.typeKey(XCUIKeyboardKeySecondaryFn, modifierFlags: [])
#else

guard let app = self.app else {
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
guard self.app != nil else {
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

let window = app.windows.firstMatch
let screenshot = window.screenshot()
guard let simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return }
let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png")
let screenshot = XCUIScreen.main.screenshot()
guard var simulator = ProcessInfo().environment["SIMULATOR_DEVICE_NAME"], let screenshotsDir = screenshotsDirectory else { return }

do {
// The simulator name contains "Clone X of " inside the screenshot file when running parallelized UI Tests on concurrent devices
let regex = try NSRegularExpression(pattern: "Clone [0-9]+ of ")
let range = NSRange(location: 0, length: simulator.count)
simulator = regex.stringByReplacingMatches(in: simulator, range: range, withTemplate: "")

let path = screenshotsDir.appendingPathComponent("\(simulator)-\(name).png")
try screenshot.pngRepresentation.write(to: path)
} catch let error {
print("Problem writing screenshot: \(name) to \(path)")
print(error)
NSLog("Problem writing screenshot: \(name) to \(screenshotsDir)/\(simulator)-\(name).png")
NSLog(error.localizedDescription)
}
#endif
}
Expand All @@ -192,7 +197,7 @@ open class Snapshot: NSObject {
#endif

guard let app = self.app else {
print("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
NSLog("XCUIApplication is not set. Please call setupSnapshot(app) before snapshot().")
return
}

Expand Down Expand Up @@ -295,4 +300,4 @@ private extension CGFloat {

// Please don't remove the lines below
// They are used to detect outdated configuration files
// SnapshotHelperVersion [1.15]
// SnapshotHelperVersion [1.21]

0 comments on commit f3da344

Please sign in to comment.