Skip to content

Commit 69c3a0c

Browse files
test: fix tests to match latest UI
1 parent 35fae50 commit 69c3a0c

3 files changed

Lines changed: 68 additions & 30 deletions

File tree

e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExample/TestView.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ struct TestView: View {
3434

3535
init() {
3636
Auth.auth().useEmulator(withHost: "localhost", port: 9099)
37-
38-
Auth.auth().settings?.isAppVerificationDisabledForTesting = true
3937
Task {
4038
try signOut()
4139
}

e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleUITests/MFAEnrolmentUITests.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ final class MFAEnrollmentUITests: XCTestCase {
196196
// 6) Select UK country code and enter phone number (without dial code)
197197
// Find and tap the country selector - try multiple approaches since it's embedded in the
198198
// TextField
199-
let countrySelector = app.buttons["🇺🇸 +1"]
199+
let countrySelector = app.buttons["phone-number-field"].firstMatch
200200
XCTAssertTrue(countrySelector.waitForExistence(timeout: 5))
201201

202202
countrySelector.tap()
@@ -229,18 +229,31 @@ final class MFAEnrollmentUITests: XCTestCase {
229229
sendCodeButton.tap()
230230

231231
// 7) Retrieve verification code from the Auth Emulator and complete setup
232-
let verificationCodeField = app.textFields["verification-code-field"]
233-
XCTAssertTrue(verificationCodeField.waitForExistence(timeout: 15))
232+
let verificationCodeField1 = app.otherElements["Digit 1 of 6"].textFields.firstMatch
233+
let verificationCodeField2 = app.otherElements["Digit 2 of 6"].textFields.firstMatch
234+
let verificationCodeField3 = app.otherElements["Digit 3 of 6"].textFields.firstMatch
235+
let verificationCodeField4 = app.otherElements["Digit 4 of 6"].textFields.firstMatch
236+
let verificationCodeField5 = app.otherElements["Digit 5 of 6"].textFields.firstMatch
237+
let verificationCodeField6 = app.otherElements["Digit 6 of 6"].textFields.firstMatch
238+
XCTAssertTrue(verificationCodeField1.waitForExistence(timeout: 15))
234239

235240
// Fetch the latest SMS verification code generated by the emulator for this phone number
236241
// The emulator stores the full phone number with dial code
237242
let fullPhoneNumber = "+44\(phoneNumberWithoutDialCode)"
238243
let code = try await getLastSmsCode(specificPhone: fullPhoneNumber)
239244

240-
UIPasteboard.general.string = code
241-
verificationCodeField.tap()
242-
verificationCodeField.press(forDuration: 1.2)
243-
app.menuItems["Paste"].tap()
245+
// Paste each digit into the corresponding text field
246+
let codeDigits = Array(code)
247+
let fields = [verificationCodeField1, verificationCodeField2, verificationCodeField3,
248+
verificationCodeField4, verificationCodeField5, verificationCodeField6]
249+
250+
for (index, digit) in codeDigits.enumerated() where index < fields.count {
251+
let field = fields[index]
252+
UIPasteboard.general.string = String(digit)
253+
field.tap()
254+
field.press(forDuration: 1.2)
255+
app.menuItems["Paste"].tap()
256+
}
244257

245258
// Test resend code button exists
246259
let resendButton = app.buttons["resend-code-button"]

e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleUITests/TestUtils.swift

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,17 @@ func createEmail() -> String {
9696
"idToken": idToken,
9797
])
9898

99-
let (_, sendResp) = try await URLSession.shared.data(for: sendReq)
99+
let (sendData, sendResp) = try await URLSession.shared.data(for: sendReq)
100100
guard let http = sendResp as? HTTPURLResponse, http.statusCode == 200 else {
101+
let errorBody = String(data: sendData, encoding: .utf8) ?? "Unknown error"
101102
throw NSError(domain: "EmulatorError", code: 1,
102-
userInfo: [NSLocalizedDescriptionKey: "Failed to send verification email"])
103+
userInfo: [NSLocalizedDescriptionKey: "Failed to send verification email: \(errorBody)"])
103104
}
104105

105-
// Step 2: Fetch OOB codes from emulator
106-
let oobURL = URL(string: "\(base)/emulator/v1/projects/\(projectID)/oobCodes")!
107-
let (oobData, oobResp) = try await URLSession.shared.data(from: oobURL)
108-
guard (oobResp as? HTTPURLResponse)?.statusCode == 200 else {
109-
throw NSError(domain: "EmulatorError", code: 2,
110-
userInfo: [NSLocalizedDescriptionKey: "Failed to fetch OOB codes"])
111-
}
106+
// Add a small delay to ensure the OOB code is registered in the emulator
107+
try await Task.sleep(nanoseconds: 500_000_000) // 0.5 seconds
112108

109+
// Define structs for OOB response parsing
113110
struct OobEnvelope: Decodable { let oobCodes: [OobItem] }
114111
struct OobItem: Decodable {
115112
let oobCode: String
@@ -118,20 +115,50 @@ func createEmail() -> String {
118115
let creationTime: String?
119116
}
120117

121-
let envelope = try JSONDecoder().decode(OobEnvelope.self, from: oobData)
122-
123-
// Step 3: Find most recent VERIFY_EMAIL code for this email
124-
let iso = ISO8601DateFormatter()
125-
let codeItem = envelope.oobCodes
126-
.filter {
127-
$0.email.caseInsensitiveCompare(email) == .orderedSame && $0.requestType == "VERIFY_EMAIL"
118+
// Step 2: Fetch OOB codes from emulator with retry logic
119+
let oobURL = URL(string: "\(base)/emulator/v1/projects/\(projectID)/oobCodes")!
120+
121+
var codeItem: OobItem?
122+
var attempts = 0
123+
let maxAttempts = 5
124+
125+
while codeItem == nil && attempts < maxAttempts {
126+
let (oobData, oobResp) = try await URLSession.shared.data(from: oobURL)
127+
guard (oobResp as? HTTPURLResponse)?.statusCode == 200 else {
128+
throw NSError(domain: "EmulatorError", code: 2,
129+
userInfo: [NSLocalizedDescriptionKey: "Failed to fetch OOB codes"])
128130
}
129-
.sorted {
130-
let d0 = $0.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
131-
let d1 = $1.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
132-
return d0 > d1
131+
132+
let envelope = try JSONDecoder().decode(OobEnvelope.self, from: oobData)
133+
134+
// Step 3: Find most recent VERIFY_EMAIL code for this email
135+
let iso = ISO8601DateFormatter()
136+
codeItem = envelope.oobCodes
137+
.filter {
138+
$0.email.caseInsensitiveCompare(email) == .orderedSame && $0.requestType == "VERIFY_EMAIL"
139+
}
140+
.sorted {
141+
let d0 = $0.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
142+
let d1 = $1.creationTime.flatMap { iso.date(from: $0) } ?? .distantPast
143+
return d0 > d1
144+
}
145+
.first
146+
147+
if codeItem == nil {
148+
attempts += 1
149+
if attempts < maxAttempts {
150+
// Wait before retrying
151+
try await Task.sleep(nanoseconds: 500_000_000) // 0.5 seconds
152+
} else {
153+
// Log available codes for debugging
154+
let availableCodes = envelope.oobCodes.map { "Email: \($0.email), Type: \($0.requestType)" }.joined(separator: "; ")
155+
throw NSError(domain: "EmulatorError", code: 3,
156+
userInfo: [
157+
NSLocalizedDescriptionKey: "No VERIFY_EMAIL OOB code found for \(email) after \(maxAttempts) attempts. Available codes: \(availableCodes)",
158+
])
159+
}
133160
}
134-
.first
161+
}
135162

136163
guard let oobCode = codeItem?.oobCode else {
137164
throw NSError(domain: "EmulatorError", code: 3,

0 commit comments

Comments
 (0)