2013-05-02 117 views
3

我想在Kif中截屏。我在kif項目的一個私有類中看到了here,這是可能的,但我很難將它轉換爲一個步驟。誰能幫忙?以Kif爲屏幕截圖

回答

2

這個搞亂了一會兒後,我已將此添加到我的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; 
    }]; 
} 
3

這在KIF 3.0.4添加。見here