UI测试点击()不起作用

时间:2016-06-10 12:08:15

标签: ios swift xcode-ui-testing

我有一个启动画面,显示3秒钟,然后淡入新视图。在这种观点中,有UILabel被称为"隐私政策"我附加了UIGestureRecognizer。我尝试使用UI测试来点击该标签以触发导航控制器推送。这在现实生活中有效,但是,在UI测试期间,水龙头不做任何事情。我的代码如下所示:

func testPrivacyPolicyLink() {
    let app = XCUIApplication()

    let exists = NSPredicate(format: "exists == 1")
    expectationForPredicate(exists, evaluatedWithObject: app.images["Logo"], handler: nil)

    waitForExpectationsWithTimeout(5) { error in
        XCTAssertNil(error, "Splash screen took too long")
        sleep(4) // Added this just to make sure the splash screen has fully faded away
        app.staticTexts["Privacy Policy"].tap() // Should trigger a navigation controller push, but doesn't do anything
    }
}

即使在为这种情况录制UI测试时,它也会调用app.staticTexts["Privacy Policy"].tap()。但是当我回放它时,它并没有起作用。任何有关这方面的帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您需要将此视图的辅助功能特征设置为“按钮”或“链接”,以便辅助功能系统清楚这些元素是可触摸的。您可以在Interface Builder中进行设置,在Utilities窗格中选择Identity inspector,然后设置Accessibility Identifier和traits类 Identity inspector screenshot

或者你可以在代码中执行此操作

self.view.accessibilityTraits = UIAccessibilityTraitButton;
self.view.accessibilityIdentifier = "departments selection";