Skip to content

Instantly share code, notes, and snippets.

@vovkasm
Last active June 30, 2021 06:55
Show Gist options
  • Save vovkasm/9620d9105d0869d1debb to your computer and use it in GitHub Desktop.
Save vovkasm/9620d9105d0869d1debb to your computer and use it in GitHub Desktop.
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
@vovkasm
Copy link
Author

vovkasm 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
Copy link

3rdcycle commented May 5, 2016

Awesome, thanks!

Copy link

ghost 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
Copy link
Author

vovkasm 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