Best EarlGrey code snippet using performAction.grey_snapshot
ScreenshotTest.m
Source:ScreenshotTest.m
...34 EDORemoteVariable<UIImage *> *snapshot = [[EDORemoteVariable alloc] init];35 EDORemoteVariable<UIImage *> *snapshotCopy = [[EDORemoteVariable alloc] init];36 // Snapshot Accessibility Element.37 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]38 performAction:grey_snapshot(snapshot)];39 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]40 performAction:grey_snapshot(snapshotCopy)];41 NSData *snapshotData = UIImagePNGRepresentation(snapshot.object);42 NSData *snapshotCopyData = UIImagePNGRepresentation(snapshotCopy.object);43 GREYAssertEqualObjects(snapshotData, snapshotCopyData, @"should be equal");44}45- (void)testSnapshotAXElementInPortraitMode {46 [self openTestViewNamed:@"Accessibility Views"];47 EDORemoteVariable<UIImage *> *snapshot = [[EDORemoteVariable alloc] init];48 // Snapshot Accessibility Element.49 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]50 performAction:grey_snapshot(snapshot)];51 // TODO: Verify the content of the image as well. // NOLINT52 CGSize expectedSize = CGSizeMake(64, 128);53 CGFloat expectedScale = [GREY_REMOTE_CLASS_IN_APP(UIScreen) mainScreen].scale;54 GREYAssertEqual(expectedSize.width, snapshot.object.size.width, @"should be equal");55 GREYAssertEqual(expectedSize.height, snapshot.object.size.height, @"should be equal");56 GREYAssertEqual(expectedScale, snapshot.object.scale, @"should be equal");57 NSError *error = nil;58 // Snapshot Accessibility Element with zero height should be an error.59 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"ElementWithZeroHeightIdentifier")]60 performAction:grey_snapshot(snapshot)61 error:&error];62 GREYAssertEqualObjects(kGREYInteractionErrorDomain, error.domain, @"should be equal");63}64- (void)testSnapshotAXElementInLandscapeMode {65 [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft error:nil];66 [self openTestViewNamed:@"Accessibility Views"];67 EDORemoteVariable<UIImage *> *snapshot = [[EDORemoteVariable alloc] init];68 // Snapshot Accessibility Element.69 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]70 performAction:grey_snapshot(snapshot)];71 // TODO: Verify the content of the image as well. // NOLINT72 CGSize expectedSize = CGSizeMake(64, 128);73 if (!iOS8_0_OR_ABOVE()) {74 // Width and height are interchanged on versions before iOS 8.075 expectedSize = CGSizeMake(expectedSize.height, expectedSize.width);76 }77 CGFloat expectedScale = [GREY_REMOTE_CLASS_IN_APP(UIScreen) mainScreen].scale;78 GREYAssertEqual(expectedSize.width, snapshot.object.size.width, @"should be equal");79 GREYAssertEqual(expectedSize.height, snapshot.object.size.height, @"should be equal");80 GREYAssertEqual(expectedScale, snapshot.object.scale, @"should be equal");81 NSError *error = nil;82 // Snapshot Accessibility Element with zero height should be an error.83 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"ElementWithZeroHeightIdentifier")]84 performAction:grey_snapshot(snapshot)85 error:&error];86 GREYAssertEqualObjects(kGREYInteractionErrorDomain, error.domain, @"should be equal");87}88// Tests to be run only on iOS 11 since we need the new XCUIScreen API.89#if defined(__IPHONE_11_0)90- (void)testTakeScreenShotForAppStoreInPortraitMode {91 [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationPortrait error:nil];92 UIImage *screenshot = [XCUIScreen mainScreen].screenshot.image;93 GREYAssert(screenshot, @"Failed to take screenshot");94 CGRect actualRect = CGRectMake(0, 0, screenshot.size.width, screenshot.size.height);95 GREYAssertTrue(CGRectEqualToRect(actualRect, [self ftr_expectedImageRectForAppStore]),96 @"Screenshot isn't correct dimension");97}98- (void)testTakeScreenShotForAppStoreInPortraitUpsideDownMode {...
FTRScreenshotTest.m
Source:FTRScreenshotTest.m
...34 EDORemoteVariable<UIImage *> *snapshot = [[EDORemoteVariable alloc] init];35 EDORemoteVariable<UIImage *> *snapshotCopy = [[EDORemoteVariable alloc] init];36 // Snapshot Accessibility Element.37 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]38 performAction:grey_snapshot(snapshot)];39 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]40 performAction:grey_snapshot(snapshotCopy)];41 NSData *snapshotData = UIImagePNGRepresentation(snapshot.object);42 NSData *snapshotCopyData = UIImagePNGRepresentation(snapshotCopy.object);43 GREYAssertEqualObjects(snapshotData, snapshotCopyData, @"should be equal");44}45- (void)testSnapshotAXElementInPortraitMode {46 [self openTestViewNamed:@"Accessibility Views"];47 EDORemoteVariable<UIImage *> *snapshot = [[EDORemoteVariable alloc] init];48 // Snapshot Accessibility Element.49 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]50 performAction:grey_snapshot(snapshot)];51 // TODO: Verify the content of the image as well. // NOLINT52 CGSize expectedSize = CGSizeMake(64, 128);53 CGFloat expectedScale = [GREY_REMOTE_CLASS_IN_APP(UIScreen) mainScreen].scale;54 GREYAssertEqual(expectedSize.width, snapshot.object.size.width, @"should be equal");55 GREYAssertEqual(expectedSize.height, snapshot.object.size.height, @"should be equal");56 GREYAssertEqual(expectedScale, snapshot.object.scale, @"should be equal");57 NSError *error = nil;58 // Snapshot Accessibility Element with zero height should be an error.59 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"ElementWithZeroHeight")]60 performAction:grey_snapshot(snapshot)61 error:&error];62 GREYAssertEqualObjects(kGREYInteractionErrorDomain, error.domain, @"should be equal");63}64- (void)testSnapshotAXElementInLandscapeMode {65 [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft error:nil];66 [self openTestViewNamed:@"Accessibility Views"];67 EDORemoteVariable<UIImage *> *snapshot = [[EDORemoteVariable alloc] init];68 // Snapshot Accessibility Element.69 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"OnScreenRectangleElementLabel")]70 performAction:grey_snapshot(snapshot)];71 // TODO: Verify the content of the image as well. // NOLINT72 CGSize expectedSize = CGSizeMake(64, 128);73 if (!iOS8_0_OR_ABOVE()) {74 // Width and height are interchanged on versions before iOS 8.075 expectedSize = CGSizeMake(expectedSize.height, expectedSize.width);76 }77 CGFloat expectedScale = [GREY_REMOTE_CLASS_IN_APP(UIScreen) mainScreen].scale;78 GREYAssertEqual(expectedSize.width, snapshot.object.size.width, @"should be equal");79 GREYAssertEqual(expectedSize.height, snapshot.object.size.height, @"should be equal");80 GREYAssertEqual(expectedScale, snapshot.object.scale, @"should be equal");81 NSError *error = nil;82 // Snapshot Accessibility Element with zero height should be an error.83 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"ElementWithZeroHeight")]84 performAction:grey_snapshot(snapshot)85 error:&error];86 GREYAssertEqualObjects(kGREYInteractionErrorDomain, error.domain, @"should be equal");87}88// Tests to be run only on iOS 11 since we need the new XCUIScreen API.89#if defined(__IPHONE_11_0)90- (void)testTakeScreenShotForAppStoreInPortraitMode {91 [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationPortrait error:nil];92 UIImage *screenshot = [XCUIScreen mainScreen].screenshot.image;93 GREYAssert(screenshot, @"Failed to take screenshot");94 CGRect actualRect = CGRectMake(0, 0, screenshot.size.width, screenshot.size.height);95 GREYAssertTrue(CGRectEqualToRect(actualRect, [self ftr_expectedImageRectForAppStore]),96 @"Screenshot isn't correct dimension");97}98- (void)testTakeScreenShotForAppStoreInPortraitUpsideDownMode {...
grey_snapshot
Using AI Code Generation
1GREYElementInteraction *interaction = [EarlGrey selectElementWithMatcher:grey_accessibilityID(@"accessibilityID")];2[interaction performAction:grey_snapshot(@"snapshotName")];3+ (id<GREYAction>)grey_snapshot:(NSString *)snapshotName {4 constraints:grey_respondsToSelector(@selector(accessibilityIdentifier))5 performBlock:^BOOL(id element, __strong NSError **errorOrNil) {6 if (![element respondsToSelector:@selector(accessibilityIdentifier)]) {7 return NO;8 }9 NSString *accessibilityIdentifier = [element accessibilityIdentifier];10 if (!accessibilityIdentifier) {11 return NO;12 }13 NSString *snapshotPath = [[EarlGreyImpl sharedEarlGrey].snapshotDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",accessibilityIdentifier]];14 [self saveImage:element toPath:snapshotPath];15 return YES;16 }];17}18+ (void)saveImage:(id)element toPath:(NSString *)path {19 UIView *view = element;20 UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);21 [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];22 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();23 UIGraphicsEndImageContext();24 [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];25}26- (NSString *)snapshotDirectory {27 if (!_snapshotDirectory) {28 _snapshotDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:@"EarlGreySnapshots"];29 NSError *error;30 [[NSFileManager defaultManager] createDirectoryAtPath:_snapshotDirectory withIntermediateDirectories:YES attributes:nil error:&error];31 if (error) {32 NSLog(@"Error creating directory at path %@: %@", _snapshotDirectory, error.localizedDescription);33 }34 }35 return _snapshotDirectory;36}37- (void)clearSnapshotDirectory {38 NSError *error;
grey_snapshot
Using AI Code Generation
1[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")]2 performAction:grey_snapshot(@"Button Snapshot", nil)];3+ (id<GREYAction>)grey_snapshot:(NSString *)name error:(__strong NSError **)errorOrNil {4 constraints:grey_respondsToSelector(@selector(accessibilityIdentifier))5 performBlock:^BOOL(id element, __strong NSError **errorOrNil) {6 NSString *path = [NSString stringWithFormat:@"%@/%@.png", kGREYOutputDirPath, name];7 [GREYScreenshotUtil grey_takeScreenshotWithName:name error:errorOrNil];8 return YES;9 }];10}11+ (id<GREYAction>)grey_snapshot:(NSString *)name error:(__strong NSError **)errorOrNil;12+ (id<GREYAction>)grey_snapshot:(NSString *)name error:(__strong NSError **)errorOrNil;13+ (id<GREYAction>)grey_snapshot:(NSString *)name error:(__strong NSError **)errorOrNil {14 constraints:grey_respondsToSelector(@selector(accessibilityIdentifier))15 performBlock:^BOOL(id element, __strong NSError **errorOrNil) {16 NSString *path = [NSString stringWithFormat:@"%@/%@.png", kGREYOutputDirPath, name];17 [GREYScreenshotUtil grey_takeScreenshotWithName:name error:errorOrNil];18 return YES;19 }];20}21+ (id<GREYAction>)grey_snapshot:(NSString *)name error:(__strong NSError **)errorOrNil;22+ (id<GREYAction>)grey_snapshot:(NSString *)name error:(__strong NSError **)errorOrNil {
grey_snapshot
Using AI Code Generation
1#import "GREYActions.h"2[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil)];3[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];4[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];5[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];6[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];7[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];8[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];9[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];10[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];11[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];12[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey_snapshot(@"snapshotName", nil) error:nil];13[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"id")] performAction:grey
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!