Skip to content

Commit

Permalink
Merge pull request #1242 from kif-framework/derko/fix-screenshot-xc13
Browse files Browse the repository at this point in the history
Remove use of XCUI for taking screenshots
  • Loading branch information
dostrander authored Nov 1, 2021
2 parents 0999601 + c8b1ebb commit fd5ebd3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion KIF.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Pod::Spec.new do |s|
s.license = 'Apache 2.0'
s.authors = 'Michael Thole', 'Eric Firestone', 'Jim Puls', 'Brian Nickel'
s.source = { :git => "https://github.com/kif-framework/KIF.git", :tag => "v#{ s.version.to_s }" }
s.platform = :ios, '9.0'
s.platform = :ios, '10.0'
s.frameworks = 'CoreGraphics', 'QuartzCore', 'IOKit', 'WebKit', 'XCTest'
s.default_subspec = 'Core'
s.requires_arc = true
Expand Down
20 changes: 18 additions & 2 deletions Sources/KIF/Additions/XCTestCase-KIFAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ - (void)writeScreenshotForException:(NSException *)exception;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[XCTContext runActivityNamed:(@"screenshot") block:^(id<XCTActivity> _Nonnull activity) {
XCUIScreenshot *screenShot = [[XCUIScreen mainScreen] screenshot];
XCTAttachment *attachment = [XCTAttachment attachmentWithScreenshot:screenShot];
XCTAttachment *attachment = [XCTAttachment attachmentWithImage:[self _snapshotScreen]];
attachment.name = @"Screenshot";
[activity addAttachment:(attachment)];
dispatch_semaphore_signal(semaphore);
}];
Expand All @@ -96,6 +96,22 @@ - (void)writeScreenshotForException:(NSException *)exception;
}
}

- (UIImage *)_snapshotScreen
{
UIView *view = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
[view layoutIfNeeded];

CGRect bounds = view.bounds;
NSAssert1(CGRectGetWidth(bounds), @"Zero width for view %@", view);
NSAssert1(CGRectGetHeight(bounds), @"Zero height for view %@", view);

UIGraphicsImageRenderer *graphicsImageRenderer = [[UIGraphicsImageRenderer alloc] initWithSize:bounds.size];

return [graphicsImageRenderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[view drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
}];
}

- (void)printViewHierarchyIfOptedIn;
{
static BOOL shouldPrint;
Expand Down

0 comments on commit fd5ebd3

Please sign in to comment.