Search code examples
iphoneiosscreenshotkif-framework

Take screenshot with Kif


I want to take a screenshot in Kif. I saw here in a private class in the kif project that it is possible, but I am having a hard time trying to convert it to a step. Can anyone help?


Solution

  • After messing with this for a bit I added this to my KIFTestStep.m

     + (id)stepToTakeScreenShotwithName:(NSString *)name;
     {
         NSString *description = [NSString stringWithFormat:@"Take a screenshot saved by the name %@", name];
    
         return [self stepWithDescription:description executionBlock:^(KIFTestStep *step, NSError **error) {
    
             NSString *outputPath = [NSString stringWithFormat:@"/Users/%@/ScreenShots", NSUserName()];
    
             NSArray *windows = [[UIApplication sharedApplication] windows];
             if (windows.count == 0) {
                 return KIFTestStepResultFailure;
             }
    
             UIGraphicsBeginImageContext([[windows objectAtIndex:0] bounds].size);
             for (UIWindow *window in windows) {
                 [window.layer renderInContext:UIGraphicsGetCurrentContext()];
             }
             UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
             UIGraphicsEndImageContext();
    
             NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
             NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
    
             outputPath = [outputPath stringByExpandingTildeInPath];
             outputPath = [outputPath stringByAppendingPathComponent:[name stringByReplacingOccurrencesOfString:@"/" withString:@"_"]];
             outputPath = [outputPath stringByAppendingString:[timeStampObj stringValue]];
             outputPath = [outputPath stringByAppendingPathExtension:@"png"];
             [UIImagePNGRepresentation(image) writeToFile:outputPath atomically:YES];
    
             return KIFTestStepResultSuccess;
        }];
     }