DaysUntilBirthdayUITests_iOS.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright 2022 Google LLC
  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
  17. class DaysUntilBirthdayUITests_iOS: XCTestCase {
  18. private let signInStaticText =
  19. "“DaysUntilBirthday (iOS)” Wants to Use “google.com” to Sign In"
  20. private let passwordManagerPrompt =
  21. "Save Password?"
  22. private let signInDisclaimerHeaderText =
  23. "Sign in to DaysUntilBirthday (iOS)"
  24. private let returningUserSignInDisclaimerHeaderText =
  25. "You’re signing back in to DaysUntilBirthday (iOS)"
  26. private let additionalAccessHeaderText = "DaysUntilBirthday (iOS) wants additional access to your Google Account"
  27. private let appTrustWarningText = "Make sure you trust DaysUntilBirthday (iOS)"
  28. private let chooseAnAccountHeaderText = "Choose an account"
  29. private let notNowText = "Not Now"
  30. private let timeout: TimeInterval = 30
  31. private let sampleApp = XCUIApplication()
  32. private let springboardApp = XCUIApplication(
  33. bundleIdentifier: "com.apple.springboard"
  34. )
  35. func testSignInNavigateToDaysUntilBirthdayAndDisconnect() {
  36. sampleApp.launch()
  37. XCTAssertTrue(signIn())
  38. XCTAssertTrue(navigateToDaysUntilBirthday())
  39. XCTAssertTrue(navigateBackToUserProfileView())
  40. sampleApp.navigationBars.buttons["Disconnect"].tap()
  41. guard sampleApp
  42. .buttons["GoogleSignInButton"]
  43. .waitForExistence(timeout: timeout) else {
  44. return XCTFail("Disconnecting should return user to sign in view")
  45. }
  46. }
  47. func testSignInAndSignOut() {
  48. sampleApp.launch()
  49. XCTAssertTrue(signIn())
  50. guard sampleApp
  51. .navigationBars
  52. .buttons["Sign Out"]
  53. .waitForExistence(timeout: timeout) else {
  54. return XCTFail("Failed to find the 'Disconnect' button")
  55. }
  56. sampleApp.buttons["Sign Out"].tap()
  57. guard sampleApp
  58. .buttons["GoogleSignInButton"]
  59. .waitForExistence(timeout: timeout) else {
  60. return XCTFail("Signing out should return user to sign in view")
  61. }
  62. }
  63. }
  64. extension DaysUntilBirthdayUITests_iOS {
  65. /// Performs a sign in.
  66. /// - returns: `true` if the sign in was successful.
  67. func signIn() -> Bool {
  68. let signInButton = sampleApp.buttons["GoogleSignInButton"]
  69. guard signInButton.exists else {
  70. XCTFail("Sign in button does not exist")
  71. return false
  72. }
  73. signInButton.tap()
  74. guard springboardApp
  75. .staticTexts[signInStaticText]
  76. .waitForExistence(timeout: timeout) else {
  77. XCTFail("Failed to display permission prompt")
  78. return false
  79. }
  80. guard springboardApp
  81. .buttons["Continue"]
  82. .waitForExistence(timeout: timeout) else {
  83. XCTFail("Failed to find 'Continue' button")
  84. return false
  85. }
  86. springboardApp.buttons["Continue"].tap()
  87. if sampleApp
  88. .staticTexts[Credential.email.rawValue]
  89. .waitForExistence(timeout: timeout) {
  90. // This email was previously used to sign in
  91. XCTAssertTrue(useExistingSignIn())
  92. } else {
  93. // This is a first time sign in
  94. XCTAssertTrue(signInForTheFirstTime())
  95. }
  96. guard sampleApp.wait(for: .runningForeground, timeout: timeout) else {
  97. XCTFail("Failed to return sample app to foreground")
  98. return false
  99. }
  100. guard sampleApp.staticTexts["User Profile"]
  101. .waitForExistence(timeout: timeout) else {
  102. XCTFail("Failed to sign in and return to app's User Profile view.")
  103. return false
  104. }
  105. return true
  106. }
  107. /// Signs in expecting the first time flow.
  108. /// @discussion
  109. /// This will assumme the full flow where a user must type in an email and
  110. /// password to sign in with.
  111. func signInForTheFirstTime() -> Bool {
  112. guard sampleApp.textFields["Email or phone"]
  113. .waitForExistence(timeout: timeout) else {
  114. XCTFail("Failed to find email textfield")
  115. return false
  116. }
  117. guard sampleApp
  118. .keyboards
  119. .element
  120. .buttons["return"]
  121. .waitForExistence(timeout: timeout) else {
  122. XCTFail("Failed to find 'return' button")
  123. return false
  124. }
  125. sampleApp.textFields["Email or phone"].typeText(Credential.email.rawValue)
  126. sampleApp.keyboards.element.buttons["return"].tap()
  127. guard sampleApp.secureTextFields["Enter your password"]
  128. .waitForExistence(timeout: timeout) else {
  129. XCTFail("Failed to find password textfield")
  130. return false
  131. }
  132. guard sampleApp
  133. .keyboards
  134. .element
  135. .buttons["return"]
  136. .waitForExistence(timeout: timeout) else {
  137. XCTFail("Failed to find 'return' button")
  138. return false
  139. }
  140. sampleApp
  141. .secureTextFields["Enter your password"]
  142. .typeText(Credential.password.rawValue)
  143. sampleApp.keyboards.element.buttons["return"].tap()
  144. if sampleApp
  145. .staticTexts[passwordManagerPrompt]
  146. .waitForExistence(timeout: timeout) {
  147. guard sampleApp
  148. .buttons[notNowText]
  149. .waitForExistence(timeout: timeout) else {
  150. XCTFail("Failed to find \(notNowText) button")
  151. return false
  152. }
  153. sampleApp.buttons[notNowText].tap()
  154. }
  155. // Proceed through sign-in disclaimer and/or access request view(s) if needed
  156. handleSignInDisclaimerIfNeeded()
  157. handleAccessRequestIfNeeded()
  158. handleReturningUserSignInDisclaimerIfNeeded()
  159. return true
  160. }
  161. /// Signs in expecting a prior sign in.
  162. /// @discussion
  163. /// This will check that there is a `Credential.email` in a list to select and
  164. /// sign in with.
  165. func useExistingSignIn() -> Bool {
  166. guard findAndTapExistingSignedInEmail() else {
  167. XCTFail("Failed to find signed-in account")
  168. return false
  169. }
  170. handleSignInDisclaimerIfNeeded()
  171. handleAccessRequestIfNeeded()
  172. handleReturningUserSignInDisclaimerIfNeeded()
  173. return true
  174. }
  175. /// Navigates to the days until birthday view from the user profile view.
  176. /// - returns: `true` if the navigation was performed successfully.
  177. /// - note: This method will attempt to find a pop up asking for permission to
  178. /// sign in with Google.
  179. func navigateToDaysUntilBirthday() -> Bool {
  180. guard sampleApp.buttons["View Days Until Birthday"]
  181. .waitForExistence(timeout: timeout) else {
  182. XCTFail("Failed to find button navigating to days until birthday view")
  183. return false
  184. }
  185. sampleApp.buttons["View Days Until Birthday"].tap()
  186. if springboardApp
  187. .staticTexts[signInStaticText]
  188. .waitForExistence(timeout: timeout) {
  189. guard springboardApp
  190. .buttons["Continue"]
  191. .waitForExistence(timeout: timeout) else {
  192. XCTFail("Failed to find 'Continue' button")
  193. return false
  194. }
  195. springboardApp.buttons["Continue"].tap()
  196. if sampleApp
  197. .staticTexts[chooseAnAccountHeaderText]
  198. .waitForExistence(timeout: timeout) {
  199. guard findAndTapExistingSignedInEmail() else {
  200. XCTFail("Failed to find signed-in account")
  201. return false
  202. }
  203. }
  204. handleAccessRequestIfNeeded()
  205. }
  206. guard sampleApp.staticTexts["Days Until Birthday"]
  207. .waitForExistence(timeout: timeout) else {
  208. XCTFail("Failed to show days until birthday view")
  209. return false
  210. }
  211. return true
  212. }
  213. /// Navigates back to the User Profile view from the Days Until Birthday View.
  214. /// - returns: `true` if the navigation was successfully performed.
  215. func navigateBackToUserProfileView() -> Bool {
  216. guard sampleApp
  217. .navigationBars
  218. .buttons["User Profile"]
  219. .waitForExistence(timeout: timeout) else {
  220. XCTFail("Failed to show navigation button back to user profile view")
  221. return false
  222. }
  223. sampleApp.navigationBars.buttons["User Profile"].tap()
  224. guard sampleApp
  225. .navigationBars
  226. .buttons["Disconnect"]
  227. .waitForExistence(timeout: timeout) else {
  228. XCTFail("Failed to find the 'Disconnect' button")
  229. return false
  230. }
  231. return true
  232. }
  233. /// Proceeds through the view with header "Days Until Birthday wants additional access to your Google Account" if needed.
  234. func handleAccessRequestIfNeeded() {
  235. let currentlyShowingAdditionalAccessRequest = sampleApp.staticTexts[additionalAccessHeaderText]
  236. .waitForExistence(timeout: timeout) && sampleApp.staticTexts[appTrustWarningText]
  237. .waitForExistence(timeout: timeout) &&
  238. sampleApp.buttons["Continue"]
  239. .waitForExistence(timeout: timeout)
  240. if currentlyShowingAdditionalAccessRequest {
  241. sampleApp.buttons["Continue"].tap()
  242. }
  243. }
  244. /// Proceeds through the sign-in disclaimer view if needed.
  245. func handleSignInDisclaimerIfNeeded() {
  246. let currentlyShowingSignInDisclaimer = sampleApp.staticTexts[signInDisclaimerHeaderText]
  247. .waitForExistence(timeout: timeout) &&
  248. sampleApp.buttons["Continue"]
  249. .waitForExistence(timeout: timeout)
  250. if currentlyShowingSignInDisclaimer {
  251. sampleApp.buttons["Continue"].tap()
  252. }
  253. }
  254. func handleReturningUserSignInDisclaimerIfNeeded() {
  255. let currentlyShowingReturningUserSignInDisclaimer =
  256. sampleApp.staticTexts[returningUserSignInDisclaimerHeaderText]
  257. .waitForExistence(timeout: timeout) &&
  258. sampleApp.buttons["Continue"]
  259. .waitForExistence(timeout: timeout)
  260. if currentlyShowingReturningUserSignInDisclaimer {
  261. sampleApp.buttons["Continue"].tap()
  262. }
  263. }
  264. /// This method looks for an account in the current view that reflects an already-signed-in state, and taps it.
  265. /// - returns: true if the signed-in account was found and tapped, otherwise returns false.
  266. func findAndTapExistingSignedInEmail() -> Bool {
  267. guard sampleApp.staticTexts[Credential.email.rawValue].exists else {
  268. XCTFail("Email used for previous sign-in was not found.")
  269. return false
  270. }
  271. guard sampleApp.staticTexts[Credential.email.rawValue].isHittable else {
  272. XCTFail("Email used for previous sign-in not tappable.")
  273. return false
  274. }
  275. sampleApp.staticTexts[Credential.email.rawValue].tap()
  276. return true
  277. }
  278. }