iOS UITests 在 Swift 3 更新后无法执行基本操作

iOS UITests can't do basics actions after Swift 3 update

在我的 iOS 中,我正在使用 XCUITest 实现 UITest。它工作得很好,因为我有 Swift 2.3,但是在将应用程序更新到 Swift 之后,tap() 等 3 个基本操作不再起作用。

只是一个不再有效的简单代码:

XCUIApplication().buttons["orgMenu"].tap()

投掷

Assertion Failure: <unknown>:0: UI Testing Failure - Failure getting snapshot Error Domain=XCTestManagerErrorDomain Code=9 "Error -25204 getting snapshot for element <AXUIElement 0x7f8297d15a50> {pid=32375}" UserInfo={NSLocalizedDescription=Error -25204 getting snapshot for element <AXUIElement 0x7f8297d15a50> {pid=32375}}

按钮的名称是正确的:如果我记录测试并点击按钮,上面的行正是我得到的。

按钮在视图中,因为我正在等待它的存在(通过断点手动尝试,并以编程方式尝试:

let exists = NSPredicate(format: "exists == 1")
expectation(for: exists, evaluatedWith: XCUIApplication().buttons["orgMenu"], handler: nil
waitForExpectations(timeout: time, handler: nil)

)

无论如何,它在 Swift 3 之前有效。 任何的想法?提前致谢!

我只回答我自己的问题,因为我找到了解决方法。

自 Swift3 起,出于某种原因,UI 测试无法使用 loadMore 模式管理视图。如果我在我的视图中有一个 TableView 并且在代码中的某处手动管理 loadMore 模式,并且在测试中我点击了一个按钮,loadMore 在一种无限循环中被调用,并且那开销应用程序的资源,导致测试失败。

解决方法:如果 UI 测试是 运行,只需停用任何 loadMore

在测试设置中:

override func setUp() {
    super.setUp()
    let app = XCUIApplication()

    continueAfterFailure = false
    app.launchEnvironment = ["isUITest":"YES"]
    app.launch()
}

在加载数据的视图中:

-(void)loadMore
{
    if ([Utils isRunningUITest]) {
        return;
    }
    // My actual function.
}

和我的实用程序

+(BOOL)isRunningUITest {
    NSDictionary *environment = [[NSProcessInfo processInfo] environment];
    return environment[@"isUITest"];
}

抱歉 Swift 和 Objective-C 混合使用,希望这对某人有帮助。

您可以尝试做类似的事情:

let app = XCUIApplication()
XCTAssert(app.buttons["orgMenu"].waitForExistence(timeout: 10))
app.buttons["orgMenu"].tap()