Skip to content

Commit 205091d

Browse files
test: use test helper for pasting into inputs
1 parent 5f28ae9 commit 205091d

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleUITests/MFAEnrolmentUITests.swift

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,11 @@ final class MFAEnrollmentUITests: XCTestCase {
211211
let phoneField = app.textFields["phone-number-field"]
212212
XCTAssertTrue(phoneField.waitForExistence(timeout: 10))
213213
let phoneNumberWithoutDialCode = "7444555666"
214-
UIPasteboard.general.string = phoneNumberWithoutDialCode
215-
phoneField.tap()
216-
phoneField.press(forDuration: 1.2)
217-
app.menuItems["Paste"].tap()
214+
try pasteIntoField(phoneField, text: phoneNumberWithoutDialCode, app: app)
218215

219216
let displayNameField = app.textFields["display-name-field"]
220217
XCTAssertTrue(displayNameField.waitForExistence(timeout: 10))
221-
UIPasteboard.general.string = "test user"
222-
displayNameField.tap()
223-
displayNameField.press(forDuration: 1.2)
224-
app.menuItems["Paste"].tap()
218+
try pasteIntoField(displayNameField, text: "test user", app: app)
225219

226220
let sendCodeButton = app.buttons["send-sms-button"]
227221
XCTAssertTrue(sendCodeButton.waitForExistence(timeout: 10))
@@ -237,10 +231,7 @@ final class MFAEnrollmentUITests: XCTestCase {
237231
let fullPhoneNumber = "+44\(phoneNumberWithoutDialCode)"
238232
let code = try await getLastSmsCode(specificPhone: fullPhoneNumber)
239233

240-
UIPasteboard.general.string = code
241-
verificationCodeField.tap()
242-
verificationCodeField.press(forDuration: 1.2)
243-
app.menuItems["Paste"].tap()
234+
try pasteIntoField(verificationCodeField, text: code, app: app)
244235

245236
// Test resend code button exists
246237
let resendButton = app.buttons["resend-code-button"]
@@ -396,16 +387,12 @@ final class MFAEnrollmentUITests: XCTestCase {
396387
let emailField = app.textFields["email-field"]
397388
XCTAssertTrue(emailField.waitForExistence(timeout: 10), "Email field should exist")
398389
// Workaround for updating SecureFields with ConnectHardwareKeyboard enabled
399-
UIPasteboard.general.string = email
400-
emailField.press(forDuration: 1.2)
401-
app.menuItems["Paste"].tap()
390+
try pasteIntoField(emailField, text: email, app: app)
402391

403392
// Fill password field
404393
let passwordField = app.secureTextFields["password-field"]
405394
XCTAssertTrue(passwordField.exists, "Password field should exist")
406-
UIPasteboard.general.string = password
407-
passwordField.press(forDuration: 1.2)
408-
app.menuItems["Paste"].tap()
395+
try pasteIntoField(passwordField, text: password, app: app)
409396

410397
// Create the user (sign up)
411398
let signUpButton = app

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleUITests/TestUtils.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,46 @@ func createEmail() -> String {
2929
}
3030
}
3131

32+
// MARK: - Text Input Helpers
33+
34+
/// Pastes text into a text field using the system paste menu
35+
/// - Parameters:
36+
/// - field: The XCUIElement representing the text field
37+
/// - text: The text to paste
38+
/// - app: The XCUIApplication instance
39+
@MainActor func pasteIntoField(_ field: XCUIElement, text: String, app: XCUIApplication) throws {
40+
UIPasteboard.general.string = text
41+
field.tap()
42+
43+
// Give field time to become first responder
44+
usleep(200_000) // 0.2 seconds
45+
46+
// Press and hold to bring up paste menu
47+
field.press(forDuration: 1.5)
48+
49+
let pasteMenuItem = app.menuItems["Paste"]
50+
51+
// Wait for paste menu to appear
52+
if !pasteMenuItem.waitForExistence(timeout: 3) {
53+
// Fallback: try double tap approach
54+
field.doubleTap()
55+
usleep(300_000) // 0.3 seconds
56+
57+
if !pasteMenuItem.waitForExistence(timeout: 2) {
58+
throw NSError(
59+
domain: "TestError",
60+
code: 1,
61+
userInfo: [
62+
NSLocalizedDescriptionKey: "Failed to show paste menu for field. Text was: \(text)"
63+
]
64+
)
65+
}
66+
}
67+
68+
pasteMenuItem.tap()
69+
70+
}
71+
3272
// MARK: - User Creation
3373

3474
/// Helper to create a test user in the emulator via REST API (avoids keychain issues)

0 commit comments

Comments
 (0)