Skip to content

Commit

Permalink
Add KIF_SCREENSHOTS_DATE_FORMAT format filename of resulting image
Browse files Browse the repository at this point in the history
1) Added code that would attempt to create a folder if KIF_SCREENSHOTS path does not exist
2) Added default filename format: HH-mm-ss - step.description
3) Added KIF_SCREENSHOTS_DATE_FORMAT environment variable that would replace the default "HH-mm-ss" portion of the filename
4) Updated README.md to reflect changes made
  • Loading branch information
Dmitry Jerusalimsky committed Sep 12, 2012
1 parent c0b7c7e commit 0a84f69
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
38 changes: 35 additions & 3 deletions Classes/KIFTestController.m
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ - (KIFTestScenario *)_nextScenarioAfterResult:(KIFTestStepResult)result;

- (void)_writeScreenshotForStep:(KIFTestStep *)step;
{
NSString *outputPath = [[[NSProcessInfo processInfo] environment] objectForKey:@"KIF_SCREENSHOTS"];
NSDictionary *env = [[NSProcessInfo processInfo] environment];
NSString *outputPath = [env objectForKey:@"KIF_SCREENSHOTS"];
if (!outputPath) {
return;
}
Expand All @@ -446,6 +447,38 @@ - (void)_writeScreenshotForStep:(KIFTestStep *)step;
return;
}

NSString *fileName = nil;

// if folder does not exist, create it
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:outputPath isDirectory:NULL]) {
BOOL dirCreated = [fileManager createDirectoryAtPath:outputPath withIntermediateDirectories:YES attributes:nil error:NULL];
if (!dirCreated) {
return;
}
}

// generate file name
// see if a file name format was specified
NSString *fileNameFormat = [env objectForKey:@"KIF_SCREENSHOTS_DATE_FORMAT"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if (fileNameFormat) {
// use format to generate file name
[formatter setDateFormat:fileNameFormat];
NSString *dateStr = [formatter stringFromDate:[NSDate date]];
fileName = dateStr;
}
else {
// use default format: mm-dd-yy - step description.png
[formatter setDateFormat:@"HH-mm-ss"];
fileName = [formatter stringFromDate:[NSDate date]];
}

[formatter release];

fileName = [fileName stringByAppendingFormat:@" - %@", step.description];
fileName = [fileName stringByAppendingPathExtension:@"png"];

UIGraphicsBeginImageContext([[windows objectAtIndex:0] bounds].size);
for (UIWindow *window in windows) {
[window.layer renderInContext:UIGraphicsGetCurrentContext()];
Expand All @@ -454,8 +487,7 @@ - (void)_writeScreenshotForStep:(KIFTestStep *)step;
UIGraphicsEndImageContext();

outputPath = [outputPath stringByExpandingTildeInPath];
outputPath = [outputPath stringByAppendingPathComponent:[step.description stringByReplacingOccurrencesOfString:@"/" withString:@"_"]];
outputPath = [outputPath stringByAppendingPathExtension:@"png"];
outputPath = [outputPath stringByAppendingPathComponent:fileName];
[UIImagePNGRepresentation(image) writeToFile:outputPath atomically:YES];
}

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ You can set a number of environment variables to unlock hidden features of KIF.

Set `KIF_SCREENSHOTS` to the full path to a folder on your computer to have KIF output a screenshot of your app as it appears when any given step fails.

### `KIF_SCREENSHOTS_DATE_FORMAT`

Set `KIF_SCREENSHOTS_DATE_FORMAT` to a date format string to prepend the screenshot filename with a timestamp. If this value is not set, default date format is used: HH-mm-ss.

### `KIF_FAILURE_FILE`

Set `KIF_FAILURE_FILE` to the full path to a file on your computer -- that need not exist -- to have KIF keep track of the failing scenarios it encounters during a test run. If any scenarios fail during a run and `KIF_FAILURE_FILE` is set, the next run will only run the scenarios that failed the previous time. Once all of the scenarios succeed again, KIF will return to running all scenarios. This is useful if you're fixing a failing scenario, as it allows you to jump right back to where the problem was.
Expand All @@ -286,6 +290,7 @@ Set `KIF_INITIAL_SKIP_COUNT` to skip a certain number of scenarios at the beginn

Set this to a value that evaluates to true to make KIF exit on the first failing scenario. This may be useful if you want to isolate failures or if your app doesn't properly recover when a test fails.


Troubleshooting
---------------

Expand Down

0 comments on commit 0a84f69

Please sign in to comment.