FBAlertTests.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <XCTest/XCTest.h>
  10. #import <WebDriverAgentLib/FBAlert.h>
  11. #import "FBConfiguration.h"
  12. #import "FBIntegrationTestCase.h"
  13. #import "FBTestMacros.h"
  14. #import "FBMacros.h"
  15. @interface FBAlertTests : FBIntegrationTestCase
  16. @end
  17. @implementation FBAlertTests
  18. - (void)setUp
  19. {
  20. [super setUp];
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. [self launchApplication];
  24. [self goToAlertsPage];
  25. [FBConfiguration disableApplicationUIInterruptionsHandling];
  26. });
  27. [self clearAlert];
  28. }
  29. - (void)tearDown
  30. {
  31. [self clearAlert];
  32. [super tearDown];
  33. }
  34. - (void)showApplicationAlert
  35. {
  36. [self.testedApplication.buttons[FBShowAlertButtonName] tap];
  37. FBAssertWaitTillBecomesTrue(self.testedApplication.alerts.count != 0);
  38. }
  39. - (void)showApplicationSheet
  40. {
  41. [self.testedApplication.buttons[FBShowSheetAlertButtonName] tap];
  42. FBAssertWaitTillBecomesTrue(self.testedApplication.sheets.count != 0);
  43. }
  44. - (void)testAlertPresence
  45. {
  46. FBAlert *alert = [FBAlert alertWithApplication:self.testedApplication];
  47. XCTAssertFalse(alert.isPresent);
  48. [self showApplicationAlert];
  49. XCTAssertTrue(alert.isPresent);
  50. }
  51. - (void)testAlertText
  52. {
  53. FBAlert *alert = [FBAlert alertWithApplication:self.testedApplication];
  54. XCTAssertNil(alert.text);
  55. [self showApplicationAlert];
  56. XCTAssertTrue([alert.text containsString:@"Magic"]);
  57. XCTAssertTrue([alert.text containsString:@"Should read"]);
  58. }
  59. - (void)testAlertLabels
  60. {
  61. FBAlert* alert = [FBAlert alertWithApplication:self.testedApplication];
  62. XCTAssertNil(alert.buttonLabels);
  63. [self showApplicationAlert];
  64. XCTAssertNotNil(alert.buttonLabels);
  65. XCTAssertEqual(1, alert.buttonLabels.count);
  66. XCTAssertEqualObjects(@"Will do", alert.buttonLabels[0]);
  67. }
  68. - (void)testClickAlertButton
  69. {
  70. FBAlert* alert = [FBAlert alertWithApplication:self.testedApplication];
  71. XCTAssertFalse([alert clickAlertButton:@"Invalid" error:nil]);
  72. [self showApplicationAlert];
  73. XCTAssertFalse([alert clickAlertButton:@"Invalid" error:nil]);
  74. FBAssertWaitTillBecomesTrue(alert.isPresent);
  75. XCTAssertTrue([alert clickAlertButton:@"Will do" error:nil]);
  76. FBAssertWaitTillBecomesTrue(!alert.isPresent);
  77. }
  78. - (void)testAcceptingAlert
  79. {
  80. NSError *error;
  81. [self showApplicationAlert];
  82. XCTAssertTrue([[FBAlert alertWithApplication:self.testedApplication] acceptWithError:&error]);
  83. FBAssertWaitTillBecomesTrue(self.testedApplication.alerts.count == 0);
  84. XCTAssertNil(error);
  85. }
  86. - (void)testAcceptingAlertWithCustomLocator
  87. {
  88. NSError *error;
  89. [self showApplicationAlert];
  90. [FBConfiguration setAcceptAlertButtonSelector:@"**/XCUIElementTypeButton[-1]"];
  91. @try {
  92. XCTAssertTrue([[FBAlert alertWithApplication:self.testedApplication] acceptWithError:&error]);
  93. FBAssertWaitTillBecomesTrue(self.testedApplication.alerts.count == 0);
  94. XCTAssertNil(error);
  95. } @finally {
  96. [FBConfiguration setAcceptAlertButtonSelector:@""];
  97. }
  98. }
  99. - (void)testDismissingAlert
  100. {
  101. NSError *error;
  102. [self showApplicationAlert];
  103. XCTAssertTrue([[FBAlert alertWithApplication:self.testedApplication] dismissWithError:&error]);
  104. FBAssertWaitTillBecomesTrue(self.testedApplication.alerts.count == 0);
  105. XCTAssertNil(error);
  106. }
  107. - (void)testDismissingAlertWithCustomLocator
  108. {
  109. NSError *error;
  110. [self showApplicationAlert];
  111. [FBConfiguration setDismissAlertButtonSelector:@"**/XCUIElementTypeButton[-1]"];
  112. @try {
  113. XCTAssertTrue([[FBAlert alertWithApplication:self.testedApplication] dismissWithError:&error]);
  114. FBAssertWaitTillBecomesTrue(self.testedApplication.alerts.count == 0);
  115. XCTAssertNil(error);
  116. } @finally {
  117. [FBConfiguration setDismissAlertButtonSelector:@""];
  118. }
  119. }
  120. - (void)testAlertElement
  121. {
  122. [self showApplicationAlert];
  123. XCUIElement *alertElement = [FBAlert alertWithApplication:self.testedApplication].alertElement;
  124. XCTAssertTrue(alertElement.exists);
  125. XCTAssertTrue(alertElement.elementType == XCUIElementTypeAlert);
  126. }
  127. - (void)testNotificationAlert
  128. {
  129. FBAlert *alert = [FBAlert alertWithApplication:self.testedApplication];
  130. XCTAssertNil(alert.text);
  131. [self.testedApplication.buttons[@"Create Notification Alert"] tap];
  132. FBAssertWaitTillBecomesTrue(alert.isPresent);
  133. XCTAssertTrue([alert.text containsString:@"Would Like to Send You Notifications"]);
  134. XCTAssertTrue([alert.text containsString:@"Notifications may include"]);
  135. }
  136. // This test case depends on the local app permission state.
  137. - (void)testCameraRollAlert
  138. {
  139. FBAlert *alert = [FBAlert alertWithApplication:self.testedApplication];
  140. XCTAssertNil(alert.text);
  141. [self.testedApplication.buttons[@"Create Camera Roll Alert"] tap];
  142. FBAssertWaitTillBecomesTrue(alert.isPresent);
  143. // "Would Like to Access Your Photos" or "Would Like to Access Your Photo Library" displayes on the alert button.
  144. XCTAssertTrue([alert.text containsString:@"Would Like to Access Your Photo"]);
  145. // iOS 15 has different UI flow
  146. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"15.0")) {
  147. [[FBAlert alertWithApplication:self.testedApplication] dismissWithError:nil];
  148. // CI env could take longer time to show up the button, thus it needs to wait a bit.
  149. XCTAssertTrue([self.testedApplication.buttons[@"Cancel"] waitForExistenceWithTimeout:30.0]);
  150. [self.testedApplication.buttons[@"Cancel"] tap];
  151. }
  152. }
  153. - (void)testGPSAccessAlert
  154. {
  155. FBAlert *alert = [FBAlert alertWithApplication:self.testedApplication];
  156. XCTAssertNil(alert.text);
  157. [self.testedApplication.buttons[@"Create GPS access Alert"] tap];
  158. FBAssertWaitTillBecomesTrue(alert.isPresent);
  159. XCTAssertTrue([alert.text containsString:@"location"]);
  160. XCTAssertTrue([alert.text containsString:@"Yo Yo"]);
  161. }
  162. @end