Files
suixinkan_ios_new/suixinkanUITests/Support/UITestLaunchConfiguration.swift
汉秋 aac8458b04 优化 UI Test 路由直达与会话复用,提升自定义 TabBar 下的稳定性。
新增启动参数直达首页与个人中心页面,引入 AppUITestRouteDriver 与 UITestSession 共享登录,并重构各模块用例的导航断言逻辑。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 11:25:36 +08:00

135 lines
4.8 KiB
Swift
Raw Permalink 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.

//
// UITestLaunchConfiguration.swift
// suixinkanUITests
//
// Created by Codex on 2026/6/26.
//
import XCTest
/// UI Test App `AppUITestLaunchState`
enum UITestLaunchConfiguration {
static let uiTestsArgument = "-suixinkan-ui-tests"
static let resetArgument = "-suixinkan-ui-tests-reset-state"
static let openMenuArgument = "-suixinkan-ui-tests-open-menu"
static let openProfileArgument = "-suixinkan-ui-tests-open-profile"
/// UI Test openMenu / openProfile
static func launchArguments(
resetState: Bool = false,
openMenu: String? = nil,
openProfile: String? = nil
) -> [String] {
var arguments = [uiTestsArgument]
if resetState {
arguments.append(resetArgument)
}
if let openMenu, !openMenu.isEmpty {
arguments.append(contentsOf: [openMenuArgument, openMenu])
} else if let openProfile, !openProfile.isEmpty {
arguments.append(contentsOf: [openProfileArgument, openProfile])
}
return arguments
}
/// XCUIApplication UI Test resetState
static func apply(
to app: XCUIApplication,
resetState: Bool = false,
openMenu: String? = nil,
openProfile: String? = nil
) {
app.launchArguments = launchArguments(
resetState: resetState,
openMenu: openMenu,
openProfile: openProfile
)
}
static let phoneEnvironmentKey = "SUI_TEST_PHONE"
static let passwordEnvironmentKey = "SUI_TEST_PASSWORD"
static let accountNameEnvironmentKey = "SUI_TEST_ACCOUNT_NAME"
/// Scheme / xcodebuild plist
static var credentials: UITestCredentials? {
if let environmentCredentials = credentialsFromEnvironment() {
return environmentCredentials
}
return credentialsFromLocalPlist()
}
///
static func requireCredentials(file: StaticString = #filePath, line: UInt = #line) throws -> UITestCredentials {
guard let credentials else {
throw XCTSkip(
"缺少 UI Test 账号。请配置环境变量 \(phoneEnvironmentKey) / \(passwordEnvironmentKey),或在本机创建 UITestCredentials.local.plist。"
)
}
return credentials
}
/// Scheme / xcodebuild
private static func credentialsFromEnvironment() -> UITestCredentials? {
let environment = ProcessInfo.processInfo.environment
guard
let phone = environment[phoneEnvironmentKey]?.trimmingCharacters(in: .whitespacesAndNewlines),
!phone.isEmpty,
let password = environment[passwordEnvironmentKey]?.trimmingCharacters(in: .whitespacesAndNewlines),
!password.isEmpty
else {
return nil
}
let accountName = environment[accountNameEnvironmentKey]?
.trimmingCharacters(in: .whitespacesAndNewlines)
return UITestCredentials(
phone: phone,
password: password,
accountName: accountName?.isEmpty == false ? accountName : nil
)
}
/// gitignore plist 便
private static func credentialsFromLocalPlist() -> UITestCredentials? {
let plistURL = localCredentialsURL
guard
FileManager.default.fileExists(atPath: plistURL.path),
let dictionary = NSDictionary(contentsOf: plistURL) as? [String: String]
else {
return nil
}
guard
let phone = dictionary[phoneEnvironmentKey]?.trimmingCharacters(in: .whitespacesAndNewlines),
!phone.isEmpty,
let password = dictionary[passwordEnvironmentKey]?.trimmingCharacters(in: .whitespacesAndNewlines),
!password.isEmpty
else {
return nil
}
let accountName = dictionary[accountNameEnvironmentKey]?
.trimmingCharacters(in: .whitespacesAndNewlines)
return UITestCredentials(
phone: phone,
password: password,
accountName: accountName?.isEmpty == false ? accountName : nil
)
}
/// plist
private static var localCredentialsURL: URL {
URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("UITestCredentials.local.plist")
}
}
/// UI Test
struct UITestCredentials: Equatable {
let phone: String
let password: String
let accountName: String?
}