Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Objective C implementation of SnapshotHelper class for Fastline Snapshot tool (https://github.com/fastlane/fastlane/tree/master/snapshot).
/* 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
@SebastianBoldt

This comment has been minimized.

Copy link

commented Dec 16, 2015

Thanks for reimplemeting the Snapshot Helper in Objc.

@fvisticot

This comment has been minimized.

Copy link

commented Jan 31, 2016

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 ?

@vovkasm

This comment has been minimized.

Copy link
Owner Author

commented Mar 16, 2016

@fvisticot, I just pushed fixed version.

  1. Updated to work with latest snapshot from fastlane (SnashotHelper.swift version 1.2)
  2. Fix bug with trash in filenames. (Forgot to add newline after printing screenshot file name)
@3rdcycle

This comment has been minimized.

Copy link

commented May 5, 2016

Awesome, thanks!

@teresa-shirkova

This comment has been minimized.

Copy link

commented Jul 20, 2016

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).

@vovkasm

This comment has been minimized.

Copy link
Owner Author

commented Aug 30, 2016

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.