【问题标题】:Waiting for focus using predicate on XCUIElement [duplicate]使用 XCUIElement 上的谓词等待焦点 [重复]
【发布时间】:2018-10-09 16:57:37
【问题描述】:

我目前正在使用 NSPredicate 来等待 XCUITest 元素上的条件,如下:

class func waitForCondition(condition: String, element: XCUIElement) -> Bool {
    let predicate = NSPredicate(format: condition)
    let expectation = XCTNSPredicateExpectation(predicate: predicate, object: element)
    let result = XCTWaiter().wait(for: [expectation], timeout: 5)
    return result == .completed
}

它适用于大多数属性,例如“exists == 1”、“isSelected == 1”、“isHittable == 1”等。 但我想要的是一种检查焦点的方法,似乎没有一个条件可以验证这一点。

说实话,即使在 Xcode 的自动完成中,我也找不到检查焦点的方法。我找到了 XCUIElementAttributes 的文档,这是 XCUIElement 类采用的接口: https://developer.apple.com/documentation/xctest/xcuielementattributes 它声明了“hasFocus”布尔值的存在。但它似乎真的不存在于文档世界之外。

为了记录,我使用的是 Xcode 9.4.1。

【问题讨论】:

    标签: xcode-ui-testing xcuitest


    【解决方案1】:

    您可以为 XCUIElement 编写扩展。类似的东西:

    extension XCUIElement { 
    
        var hasFocus: Bool {
            return self.value(forKey: "hasKeyboardFocus") as? Bool ?? false
        }
    
    }
    

    这将返回真/假。 你可以这样使用它:

    if element.hasFocus == false {
        element.tap()
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多