-
-
Save vovkasm/9620d9105d0869d1debb to your computer and use it in GitHub Desktop.
/* This SnapshotHelper class should be compatible with SnapshotHelper.swift version 1.2 */ | |
@import Foundation; | |
@import XCTest; | |
@interface SnapshotHelper : NSObject | |
- (instancetype)initWithApp:(XCUIApplication*)app; | |
- (void)snapshot:(NSString*)name waitForLoadingIndicator:(BOOL)wait; | |
@end |
#import "SnapshotHelper.h" | |
@implementation SnapshotHelper { | |
XCUIApplication* _app; | |
NSString* _deviceLanguage; | |
NSString* _locale; | |
} | |
- (instancetype)initWithApp:(XCUIApplication *)app { | |
if ((self = [super init])) { | |
_app = app; | |
[self setLanguage]; | |
[self setLocale]; | |
[self setLaunchArguments]; | |
} | |
return self; | |
} | |
- (void)snapshot:(NSString *)name waitForLoadingIndicator:(BOOL)wait { | |
if (wait) { | |
[self waitForLoadingIndicatorToDisappear]; | |
} | |
printf("snapshot: %s\n", name.UTF8String); | |
sleep(1); | |
[XCUIDevice sharedDevice].orientation = UIDeviceOrientationUnknown; | |
} | |
- (void)waitForLoadingIndicatorToDisappear { | |
XCUIElementQuery* query = [[[_app.statusBars childrenMatchingType:XCUIElementTypeOther] elementBoundByIndex:1] childrenMatchingType:XCUIElementTypeOther]; | |
while (query.count > 4) { | |
sleep(1); | |
NSLog(@"Number of Elements in Status Bar: %ld... waiting for status bar to disappear", query.count); | |
} | |
} | |
- (void)setLanguage { | |
NSString* prefix = [self pathPrefix]; | |
if (prefix == nil) { | |
return; | |
} | |
NSString* path = [prefix stringByAppendingPathComponent:@"language.txt"]; | |
NSError* error; | |
_deviceLanguage = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; | |
if (error) { | |
NSLog(@"%s could not detect language %@", __PRETTY_FUNCTION__, error); | |
return; | |
} | |
_deviceLanguage = [_deviceLanguage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
_app.launchArguments = [_app.launchArguments arrayByAddingObjectsFromArray:@[@"-AppleLanguages",[NSString stringWithFormat:@"(%@)", _deviceLanguage]]]; | |
} | |
- (void)setLocale { | |
NSString* prefix = [self pathPrefix]; | |
if (prefix == nil) { | |
return; | |
} | |
NSString* path = [prefix stringByAppendingPathComponent:@"locale.txt"]; | |
NSError* error; | |
_locale = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; | |
if (error) { | |
NSLog(@"%s could not detect locale %@", __PRETTY_FUNCTION__, error); | |
return; | |
} | |
_locale = [_locale stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; | |
if (_locale.length == 0) { | |
_locale = [NSLocale localeWithLocaleIdentifier:_deviceLanguage].localeIdentifier; | |
} | |
_app.launchArguments = [_app.launchArguments arrayByAddingObjectsFromArray:@[@"-AppleLocale", [NSString stringWithFormat:@"\"%@\"", _locale]]]; | |
} | |
- (void)setLaunchArguments { | |
NSString* prefix = [self pathPrefix]; | |
if (prefix == nil) { | |
return; | |
} | |
NSString* path = [prefix stringByAppendingPathComponent:@"snapshot-launch_arguments.txt"]; | |
_app.launchArguments = [_app.launchArguments arrayByAddingObjectsFromArray:@[@"-FASTLANE_SNAPSHOT", @"YES", @"-ui_testing"]]; | |
NSError* error; | |
NSString* argsString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; | |
if (error) { | |
NSLog(@"%s could not detect launch arguments: %@", __PRETTY_FUNCTION__, error); | |
return; | |
} | |
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(\\\".+?\\\"|\\S+)" options:0 error:&error]; | |
if (error) { | |
NSLog(@"%s could not detect launch arguments: %@", __PRETTY_FUNCTION__, error); | |
return; | |
} | |
NSArray<NSTextCheckingResult*>* matches = [regex matchesInString:argsString options:0 range:NSMakeRange(0, argsString.length)]; | |
NSMutableArray<NSString*>* results = [NSMutableArray array]; | |
for (NSTextCheckingResult* match in matches) { | |
[results addObject:[argsString substringWithRange:match.range]]; | |
} | |
if (results.count > 0) { | |
_app.launchArguments = [_app.launchArguments arrayByAddingObjectsFromArray:results]; | |
} | |
} | |
- (NSString*)pathPrefix { | |
NSString* path = [[[NSProcessInfo processInfo] environment] objectForKey:@"SIMULATOR_HOST_HOME"]; | |
if (path == nil) { | |
NSLog(@"Couldn't find Snapshot configuration files at ~/Library/Caches/tools.fastlane"); | |
return nil; | |
} | |
return [path stringByAppendingPathComponent:@"Library/Caches/tools.fastlane"]; | |
} | |
@end |
I try your file. the snapshot is OK but the output filename is './screenshots/fr-FR/iPhone6-01BeaconsScreen t = 17.78s Set device orientation to Unknown.png' any idea ? how to set orientation in the filename ?
@fvisticot, I just pushed fixed version.
- Updated to work with latest snapshot from fastlane (SnashotHelper.swift version 1.2)
- Fix bug with trash in filenames. (Forgot to add newline after printing screenshot file name)
Awesome, thanks!
Hi! Thanks for this implementation of Snapshot. I'm having a bit of trouble integrating this with my UITests - I get the "Couldn't find Snapshot configuration files at ~/Library/Caches/tools.fastlane" error, so I think that it is not finding the configuration file correctly. When I was trying to use Snapshot's Swift version, I would get a SnapshotHelper.swift and Snapfile after running "snapshot init". Does the Snapfile need to be somewhere specific, or is there a way to provide a configuration for Snapshot when using the Objective C version?
Thank you for your patience with my question - I don't have a background in Objective C / Swift (I'm a technical marketer programming other languages but I'm working on something that requires a screenshot generator for an iOS app).
@teresa-murphy-skvorzova, sorry for late response (gists does not send notifications about comments).
As you can see in SnapshotHelper.m line 109, this message will be shown when SIMULATOR_HOST_HOME
was not set. Do you try to use this code on real device? It should work only on simulator.
Thanks for reimplemeting the Snapshot Helper in Objc.