3

我想在 Kif 中截屏。我在 kif 项目的一个私人课程中看到是可能的,但我很难将其转换为一个步骤。任何人都可以帮忙吗?

4

2 回答 2

3

这是在 KIF 3.0.4 中添加的。见这里

于 2014-05-20T00:55:45.750 回答
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;
    }];
 }
于 2013-05-02T16:41:04.910 回答