Best EarlGrey code snippet using performAction.grey_longPressWithDuration
BasicInteractionTest.m
Source:BasicInteractionTest.m  
...449      performAction:grey_turnSwitchOn(NO)];450  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Simple Label")]451      assertWithMatcher:grey_text(@"OFF")];452  [[[EarlGrey selectElementWithMatcher:grey_text(@"Long Press")]453      performAction:grey_longPressWithDuration(1.1f)] assertWithMatcher:grey_notVisible()];454  [[[EarlGrey selectElementWithMatcher:grey_text(@"Double Tap")] performAction:grey_doubleTap()]455      assertWithMatcher:grey_notVisible()];456}457/**458 * Checks a custom action.459 */460- (void)testEarlGreyInvocationInsideCustomAction {461  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];462  id<GREYAction> action =463      [[GREYHostApplicationDistantObject sharedInstance] actionForCheckingIfElementHidden];464  NSError *error;465  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:action error:&error];466  if (!error) {467    [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];468    [[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Long Press")]469        performAction:grey_longPressWithDuration(1.1f)] assertWithMatcher:grey_nil()];470  } else {471    GREYFail(@"Element should exist. We should not be here.");472  }473}474/**475 * Checks a custom assertion.476 */477- (void)testEarlGreyInvocationInsideCustomAssertion {478  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];479  id<GREYAssertion> assertion =480      [[GREYHostApplicationDistantObject sharedInstance] assertionForCheckingIfElementPresent];481  NSError *error;482  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] assert:assertion error:&error];483  if (!error) {484    [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];485    [[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Long Press")]486        performAction:grey_longPressWithDuration(1.1f)] assertWithMatcher:grey_nil()];487  } else {488    GREYFail(@"Element should exist. We should not be here.");489  }490}491/**492 * Tests a long press at a point.493 */494- (void)testLongPressAtPointOnAccessibilityElement {495  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];496  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];497  [[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Long Press")]498      performAction:grey_longPressAtPointWithDuration(CGPointMake(10, 10), 1.1f)]499      assertWithMatcher:grey_nil()];500}501/**502 * Checks long press on a text field.503 */504- (void)testLongPressOnTextField {505  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];506  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];507  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"foo")]508      performAction:grey_longPressWithDuration(1.0f)];509  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] assertWithMatcher:grey_notNil()];510}511/**512 * Check long pressing followed by selecting a menu option.513 */514- (void)testLongPressFollowedBySelectingMenuOption {515  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];516  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];517  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"foo")]518      performAction:grey_typeText(@"Hello")];519  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"foo")]520      performAction:grey_longPressAtPointWithDuration(CGPointMake(1, 1), 1.0f)];521  [[EarlGrey selectElementWithMatcher:grey_text(@"Select")] performAction:grey_tap()];522  [[EarlGrey selectElementWithMatcher:grey_text(@"Cut")] performAction:grey_tap()];...FTRBasicInteractionTest.m
Source:FTRBasicInteractionTest.m  
...440      performAction:grey_turnSwitchOn(NO)];441  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Simple Label")]442      assertWithMatcher:grey_text(@"OFF")];443  [[[EarlGrey selectElementWithMatcher:grey_text(@"Long Press")]444      performAction:grey_longPressWithDuration(1.1f)] assertWithMatcher:grey_notVisible()];445  [[[EarlGrey selectElementWithMatcher:grey_text(@"Double Tap")] performAction:grey_doubleTap()]446      assertWithMatcher:grey_notVisible()];447}448/**449 * Checks a custom action.450 */451- (void)testEarlGreyInvocationInsideCustomAction {452  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];453  id<GREYAction> action =454      [[GREYHostApplicationDistantObject sharedInstance] actionForCheckingIfElementHidden];455  NSError *error;456  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:action error:&error];457  if (!error) {458    [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];459    [[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Long Press")]460        performAction:grey_longPressWithDuration(1.1f)] assertWithMatcher:grey_nil()];461  } else {462    GREYFail(@"Element should exist. We should not be here.");463  }464}465/**466 * Checks a custom assertion.467 */468- (void)testEarlGreyInvocationInsideCustomAssertion {469  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];470  id<GREYAssertion> assertion =471      [[GREYHostApplicationDistantObject sharedInstance] assertionForCheckingIfElementPresent];472  NSError *error;473  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] assert:assertion error:&error];474  if (!error) {475    [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];476    [[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Long Press")]477        performAction:grey_longPressWithDuration(1.1f)] assertWithMatcher:grey_nil()];478  } else {479    GREYFail(@"Element should exist. We should not be here.");480  }481}482/**483 * Tests a long press at a point.484 */485- (void)testLongPressAtPointOnAccessibilityElement {486  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];487  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];488  [[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Long Press")]489      performAction:grey_longPressAtPointWithDuration(CGPointMake(10, 10), 1.1f)]490      assertWithMatcher:grey_nil()];491}492/**493 * Checks long press on a text field.494 */495- (void)testLongPressOnTextField {496  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];497  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];498  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"foo")]499      performAction:grey_longPressWithDuration(1.0f)];500  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] assertWithMatcher:grey_notNil()];501}502/**503 * Check long pressing followed by selecting a menu option.504 */505- (void)testLongPressFollowedBySelectingMenuOption {506  [[EarlGrey selectElementWithMatcher:grey_text(@"Basic Views")] performAction:grey_tap()];507  [[EarlGrey selectElementWithMatcher:grey_text(@"Tab 2")] performAction:grey_tap()];508  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"foo")]509      performAction:grey_typeText(@"Hello")];510  [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"foo")]511      performAction:grey_longPressAtPointWithDuration(CGPointMake(1, 1), 1.0f)];512  [[EarlGrey selectElementWithMatcher:grey_text(@"Select")] performAction:grey_tap()];513  [[EarlGrey selectElementWithMatcher:grey_text(@"Cut")] performAction:grey_tap()];...GestureTest.m
Source:GestureTest.m  
...69      assertWithMatcher:grey_sufficientlyVisible()];70}71- (void)testLongPressWithDuration {72  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Grey Box")]73      performAction:grey_longPressWithDuration(1.0)];74  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"single long press")]75      assertWithMatcher:grey_sufficientlyVisible()];76}77- (void)testLongPressWithDurationAtPoint {78  EDORemoteVariable<NSValue *> *remoteBounds = [[EDORemoteVariable alloc] init];79  id<GREYAction> boundsFinder =80      [GREYHostApplicationDistantObject.sharedInstance actionForFindingElementBounds:remoteBounds];81  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Grey Box")]82      performAction:boundsFinder];83  CGRect targetBounds = remoteBounds.object.CGRectValue;84  // Verify tapping outside the bounds does not cause long press.85  CGFloat midX = CGRectGetMidX(targetBounds);86  CGFloat midY = CGRectGetMidY(targetBounds);87  CGPoint outsidePoints[4] = {CGPointMake(CGRectGetMinX(targetBounds) - 1, midY),...FTRGestureTest.m
Source:FTRGestureTest.m  
...40      assertWithMatcher:grey_sufficientlyVisible()];41}42- (void)testLongPress {43  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Grey Box")]44      performAction:grey_longPressWithDuration(0.5f)];45  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"single long press")]46      assertWithMatcher:grey_sufficientlyVisible()];47}48- (void)testLongPressAtPoint {49  // Find the bounds of the element.50  __block CGRect targetBounds;51  GREYActionBlock *boundsFinder =52      [[GREYActionBlock alloc] initWithName:@"Frame finder"53                                constraints:nil54                               performBlock:^BOOL(UIView *view, NSError *__strong *error) {55    targetBounds = view.bounds;56    return YES;57  }];58  [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"Grey Box")]...GSCXTouchActivitySourceTests.m
Source:GSCXTouchActivitySourceTests.m  
...105 * @param duration The number of seconds the touch should last.106 */107- (void)gscxtest_performTouchOnAppWithDuration:(CFTimeInterval)duration {108  [[EarlGrey selectElementWithMatcher:grey_keyWindow()]109      performAction:grey_longPressWithDuration(duration)];110}111@end...grey_longPressWithDuration
Using AI Code Generation
1[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")]2    .usingSearchAction(grey_scrollInDirection(kGREYDirectionDown, 100), grey_tap())3    .performAction(grey_longPressWithDuration(5));4EarlGrey.selectElement(with: grey_accessibilityID("button"))5    .usingSearchAction(grey_scrollInDirection(kGREYDirectionDown, 100), grey_tap())6    .performAction(grey_longPressWithDuration(5))7EarlGrey.selectElement(with: grey_accessibilityID("button"))8    .usingSearchAction(grey_scrollInDirection(kGREYDirectionDown, 100), grey_tap())9    .performAction(grey_longPressWithDuration(5))grey_longPressWithDuration
Using AI Code Generation
1[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_longPressWithDuration(1.0)];2[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_doubleTap()];3[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_tap()];4[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_doubleTap()];5[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_tap()];6[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_longPress()];7[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_tap()];8[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_tap()];9[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_tap()];10[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_tap()];11[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"button")] performAction:grey_tap()];grey_longPressWithDuration
Using AI Code Generation
1[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_longPressWithDuration(2.0)];2[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_flickFastInDirection(kGREYDirectionLeft)];3[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_typeText(@"Text")];4[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_clearText()];5[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_replaceText(@"Text")];6[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_scrollInDirection(kGREYDirectionDown, 200)];7[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_scrollToContentEdge(kGREYContentEdgeTop)];8[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_tapAtPoint(CGPointMake(10, 10))];9[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_longPressAtPoint(CGPointMake(10, 10), 2.0)];10[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"buttonID")] performAction:grey_multiFingerTapWithCount(2)];grey_longPressWithDuration
Using AI Code Generation
1[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"myButton")] performAction:grey_longPressWithDuration(3.0)];2EarlGrey.selectElement(with: grey_accessibilityID("myButton")).perform(grey_longPressWithDuration(3.0))3EarlGreyImpl.invoked(fromFile: #file, lineNumber: #line).selectElement(with: grey_accessibilityID("myButton")).perform(grey_longPressWithDuration(3.0))4[[GREYActions actionForLongPressWithDuration:3.0] perform:myView error:nil];5+ (id<GREYAction>)actionForLongPressWithDuration:(CFTimeInterval)duration {6                                  constraints:grey_not(grey_systemAlertViewShown())7                              performBlock:^BOOL(id element, __strong NSError **errorOrNil) {8                                                           withErrorOrNil:errorOrNil];9                              }];10}11+ (BOOL)grey_longPressWithDuration:(CFTimeInterval)duration12                     withElement:(id)element13                  withErrorOrNil:(__strong NSError **)errorOrNil {14  CGPoint centerPoint = [element grey_centerOfVisibleArea];15  UIWindow *window = [element window];16  CGPoint windowCenterPoint = [window grey_centerOfVisibleArea];17  CGPoint elementCenterPoint = CGPointMake(centerPoint.x - windowCenterPoint.x,18                                           centerPoint.y - windowCenterPoint.y);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!!
