Files
suixinkan_ios_new/suixinkanUITests/Support/LoginFlow.swift
汉秋 d27438b264 补充 Auth/Tasks/ScenicPermission/Live 单元测试,并完善 UI Test 登录与会话稳定性。
移除 LoginViewModel 硬编码账号,扩展登录流程测试覆盖;调整 ZLoginSmokeUITests 执行顺序与路由直达逻辑,避免清空 Keychain 影响其它用例。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 12:44:10 +08:00

125 lines
4.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// LoginFlow.swift
// suixinkanUITests
//
// Created by Codex on 2026/6/26.
//
import XCTest
///
enum LoginFlow {
///
static func loginIfNeeded(
app: SuixinkanApp,
credentials: UITestCredentials,
file: StaticString = #filePath,
line: UInt = #line
) {
guard app.isOnLoginScreen else { return }
app.application.activate()
app.waitForGlobalLoadingToDisappear()
fillLoginForm(app: app, credentials: credentials)
submitLogin(app: app)
handleAccountSelectionIfNeeded(app: app, credentials: credentials)
app.waitForGlobalLoadingToDisappear()
XCTAssertTrue(
app.isOnMainTabs,
"登录后应进入主 Tab。",
file: file,
line: line
)
}
///
static func fillLoginForm(app: SuixinkanApp, credentials: UITestCredentials) {
let usernameField = app.application.textFields["login.username"]
XCTAssertTrue(usernameField.waitForExistence(timeout: 12))
let currentPhone = usernameField.value as? String
if currentPhone != credentials.phone {
tapWhenReady(usernameField)
usernameField.clearAndType(credentials.phone)
}
let passwordField = preferredPasswordField(in: app.application)
XCTAssertTrue(passwordField.waitForExistence(timeout: 4))
tapWhenReady(passwordField)
passwordField.clearAndType(credentials.password)
let privacyToggle = app.application.buttons["login.privacy"]
if privacyToggle.exists, privacyToggle.isHittable {
privacyToggle.tap()
}
}
///
static func submitLogin(app: SuixinkanApp) {
let submitButton = app.application.buttons["login.submit"]
XCTAssertTrue(submitButton.waitForExistence(timeout: 4))
tapWhenReady(submitButton)
app.waitForGlobalLoadingToDisappear()
}
/// v9
static func handleAccountSelectionIfNeeded(
app: SuixinkanApp,
credentials: UITestCredentials
) {
let accountSelectionTitle = app.application.navigationBars["选择账号"]
guard accountSelectionTitle.waitForExistence(timeout: 6) else { return }
if let accountName = credentials.accountName {
let accountCell = app.application.staticTexts[accountName]
if accountCell.waitForExistence(timeout: 4) {
accountCell.tap()
}
}
let confirmButton = app.application.buttons["进入系统"]
XCTAssertTrue(confirmButton.waitForExistence(timeout: 4))
confirmButton.tap()
app.waitForGlobalLoadingToDisappear()
}
///
private static func preferredPasswordField(in app: XCUIApplication) -> XCUIElement {
let secureField = app.secureTextFields["login.password"]
if secureField.exists {
return secureField
}
return app.textFields["login.password"]
}
/// 退
private static func tapWhenReady(_ element: XCUIElement) {
let deadline = Date().addingTimeInterval(8)
while Date() < deadline {
if element.isHittable {
element.tap()
return
}
RunLoop.current.run(until: Date().addingTimeInterval(0.2))
}
element.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5)).tap()
}
}
private extension XCUIElement {
///
func clearAndType(_ text: String) {
guard let currentValue = value as? String, !currentValue.isEmpty else {
typeText(text)
return
}
tap()
let deleteString = String(repeating: XCUIKeyboardKey.delete.rawValue, count: currentValue.count)
typeText(deleteString)
typeText(text)
}
}