FDLBuilderTestAppObjCEarlGreyTests.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #ifdef COCOAPODS
  18. #import <EarlGrey/EarlGrey.h>
  19. #else
  20. #import "third_party/objective_c/EarlGrey/EarlGrey/EarlGrey.h"
  21. #endif
  22. @interface FDLBuilderTestAppObjCEarlGreyTests : XCTestCase
  23. @end
  24. @implementation FDLBuilderTestAppObjCEarlGreyTests
  25. #pragma mark - Tests
  26. - (void)testOpenFDLFromAppGeneratedLink {
  27. // On first launch, a null FDL Received alert is displayed (by design); in
  28. // this case, we need to dismiss it in order to proceed
  29. BOOL hasFirstInstallAlertDisplayed = [self confirmPresenceOfFDLAlertWithURL:@"(null)"
  30. matchType:@"0"
  31. minimumAppVersion:@"(null)"];
  32. if (hasFirstInstallAlertDisplayed) {
  33. [[EarlGrey selectElementWithMatcher:[GREYMatchers matcherForText:@"Dismiss"]]
  34. performAction:grey_tap()];
  35. }
  36. // Scroll down in the app until the Generate Link button can be pressed, then tap it
  37. [[[EarlGrey selectElementWithMatcher:[GREYMatchers matcherForText:@"Generate Link"]]
  38. usingSearchAction:grey_swipeFastInDirection(kGREYDirectionUp)
  39. onElementWithMatcher:grey_kindOfClass([UITableView class])] performAction:grey_tap()];
  40. // Find long link table view cell
  41. NSString *fdlLongLinkId = @"LinkTableViewCell-LinkTextView-Long link";
  42. // Find long link table view cell
  43. [[[EarlGrey selectElementWithMatcher:grey_accessibilityID(fdlLongLinkId)] atIndex:0]
  44. assert:[GREYAssertionBlock
  45. assertionWithName:@"Long link non empty and valid"
  46. assertionBlockWithError:^BOOL(id element, NSError *__strong *errorOrNil) {
  47. XCTAssertTrue([element isKindOfClass:[UITextView class]]);
  48. UITextView *longLinkTextView = element;
  49. // ensure long link cell has non empty value
  50. XCTAssertTrue(longLinkTextView.text.length > 0);
  51. // ensure long link cell value is a valid URL
  52. XCTAssertNotNil([NSURL URLWithString:longLinkTextView.text]);
  53. return YES;
  54. }]];
  55. }
  56. #pragma mark - Private
  57. - (BOOL)waitForElementWithMatcher:(id<GREYMatcher>)matcher
  58. toBeVisibleWithinTime:(CFTimeInterval)timeInterval {
  59. return [[GREYCondition conditionWithName:@"Waiting for element to appear"
  60. block:^BOOL() {
  61. return [self isElementPresentWithMatcher:matcher];
  62. }] waitWithTimeout:timeInterval];
  63. }
  64. - (BOOL)isElementPresentWithMatcher:(id<GREYMatcher>)matcher {
  65. NSError *error = nil;
  66. [[[EarlGrey selectElementWithMatcher:matcher] atIndex:0] assertWithMatcher:grey_notNil()
  67. error:&error];
  68. if (error && (![error.domain isEqualToString:kGREYInteractionErrorDomain] ||
  69. error.code != kGREYInteractionElementNotFoundErrorCode)) {
  70. GREYFail(@"Unexpected error when trying to locate an element matching %@: %@", matcher, error);
  71. }
  72. return error == nil;
  73. }
  74. - (BOOL)confirmPresenceOfFDLAlertWithURL:(NSString *)URL
  75. matchType:(NSString *)matchType
  76. minimumAppVersion:(NSString *)minimumAppVersion {
  77. id<GREYMatcher> alertViewClass = grey_kindOfClass(NSClassFromString(@"_UIAlertControllerView"));
  78. NSString *expectedAlertText =
  79. [NSString stringWithFormat:@"URL [%@], matchType [%@], minimumAppVersion [%@]", URL,
  80. matchType, minimumAppVersion];
  81. return [self waitForElementWithMatcher:grey_allOf(grey_ancestor(alertViewClass),
  82. grey_text(expectedAlertText), nil)
  83. toBeVisibleWithinTime:10];
  84. }
  85. @end