ios – XCUIElement tap()不工作

我有一个非常简单的XCTestCase实现,它测试按下按钮并期望出现一个Alert控制器.问题是tap()方法不起作用.在相关按钮的IBAction中放置一个断点我意识到逻辑甚至没有被调用.
class uitestsampleUITests: XCTestCase {

    var app: XCUIApplication!

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        app = XCUIApplication()
        app.launch()
    }

    func testButton() {
        let button = app.buttons["Button"]
        button.tap()

        expectationForPredicate(NSPredicate(format: "exists == 1"),evaluatedWithObject: button,handler: nil)
        waitForExpectationsWithTimeout(5.0,handler: nil)
    }
}

另外,复制button.tap()指令会使测试通过,如下所示:

func testButton() {
        let button = app.buttons["Button"]
        button.tap()
        button.tap()

        expectationForPredicate(NSPredicate(format: "exists == 1"),handler: nil)    
    }

我在Xcode 7.3.1中遇到这个问题我错过了什么吗?这是一个错误吗?

解决方法

因此,一位Apple工程师回复了我的错误报告:

The second possibility is that you are running into a problem that
sometimes occurs where the application finishes launching but the
splash screen doesn’t immediately disappear and events dispatched to
the app are not handled properly.

To try to work around that issue,consider placing a small delay at
the beginning of your test (sleep(1) should be enough).

所以我做了它现在它的工作原理:

override func setUp() {
    super.setUp()
    continueAfterFailure = false
    app = XCUIApplication()
    app.launch()
    sleep(1)
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...