我目前正在使用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“Bool的存在。但它似乎真的不存在于文档世界之外。
根据记录,我使用的是Xcode 9.4.1。
发布于 2018-11-06 19:00:05
您可以为XCUIElement编写一个扩展。类似于:
extension XCUIElement {
var hasFocus: Bool {
return self.value(forKey: "hasKeyboardFocus") as? Bool ?? false
}
}这将返回true/false。你可以这样使用它:
if element.hasFocus == false {
element.tap()
}https://stackoverflow.com/questions/52725936
复制相似问题