我使用特殊的服务器来替代真实的 url 服务器响应(如 Swifter)。在应用程序启动之前,我将一堆 url 路径和新的替换 url 发送到 launchEnvironment:
func launchNewApp(serverManager: ServerManagerProtocol) -> XCUIApplication {
let app = XCUIApplication()
app.launchArguments += ["UI-TESTING"]
serverManager.routeInfo.forEach {
app.launchEnvironment[$0.key] = $0.value
}
app.launch()
return app
}
我的测试启动应用程序,在我的应用程序中我替换了网址,我的应用程序得到了模拟的服务器响应:
extension URLRequest {
private var isUITestingLaunching: Bool {
ProcessInfo.processInfo.arguments.contains("UI-TESTING")
}
mutating func tryReplaceUITestRequest() {
#if DEBUG
if isUITestingLaunching,
let path = url?.path,
let substitutionuUrlString = ProcessInfo.processInfo.environment[path],
let substitutionuUrl = URL(string: substitutionuUrlString) {
url = substitutionuUrl
}
#endif
}
}
现在我希望在测试运行时替换模拟的 url:
我设置了启动环境并启动应用程序
我分析从模拟响应的数据中收到的 UI
我更改了启动环境中的可能网址
我确实在应用程序中拉动刷新
我看到新的 UI(因为在新的模拟 URL 响应中接收新数据)
func testExample() throws {
// 1 launch app
let app = XCUIApplication()
app.launchArguments += ["UI-TESTING"]
app.launchEnvironment["HomeSceneStructureUrlPath"] = "http://localhost:9000/SomeUrlPath"
app.launch()
// 2 test UI
...
XCTAssertFalse(someElement.waitForExistence(timeout: 100))
// 3 change launchEnvironment
app.launchEnvironment["HomeSceneStructureUrlPath"] = "http://localhost:9000/OtherUrlPath"
// 4 do PULL TO REFRESH
let firstCell = app.tables["someTable"].cells.firstMatch
let start = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
let finish = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 10))
start.press(forDuration: 0, thenDragTo: finish)
// 5 test updating UI
...
XCTAssertTrue(settingsButton.waitForExistence(timeout: 100))
}
但是我的步骤3不起作用,我无法在测试运行时更改launchEnvironment。您有什么想法可以做到这一点吗?或者也许还有另一种方法将模拟 url 从测试目标发送到项目目标?我将不胜感激您的答复。
根据Apple 文档,更改环境变量不会对当前启动的应用程序生效:
与Process不同,修改环境变量也是合法的 应用程序启动后。此类更改不会影响 当前启动会话,但将在下次启动时生效 应用程序已启动。
除了黑客/模拟任何类正在执行调用以在收到第一个响应时调用更新的 URL 之外,我不确定更新 UI 的好方法。