ios - Asynchronous UI testing in Swift
问题描述
I have a button in a UI test that logs in to an app. The user flow to automate is this:
- User taps button
- Button text changes from "Log In" to "Processing..."
- Button disappears when log in is successful
My UI test code is very simple for the functional test:
let loginButton = app.buttons["LOG IN"]
let welcomeText = app.staticTexts["Welcome!"]
loginButton.tap()
XCTAssertTrue(welcomeText.waitForExistence(10))
However, it does not validate that the button changes state to "Processing...".
If I attempt to put a check in the code like this, it doesn't work because by the time the UI Test has walked the screen looking for the text, it might have disappeared.
let loginButton = app.buttons["LOG IN"]
let loginButtonProcessingText = app.buttons.staticTexts["Processing..."]
let welcomeText = app.staticTexts["Welcome!"]
loginButton.tap()
XCTAssertTrue(loginButtonProcessingText.exists)
XCTAssertTrue(welcomeText.waitForExistence(10))
So my thought is that I need to somehow start the check for the processing test simultaneously with the button tap, or maybe a second before it.
Is there a way to spin up a check like that asynchronously in XCUI testing?
解决方案
您可以使用期望来测试异步任务。但首先,如果您将步骤分开,如下所示会更清楚;
let loginButton = app.buttons["LOG IN"]
loginButton.tap()
let loginButtonProcessingText = app.buttons.staticTexts["Processing..."]
XCTAssertTrue(loginButtonProcessingText.exists)
let welcomeText = app.staticTexts["Welcome!"]
XCTAssertTrue(welcomeText.waitForExistence(10))