有没有办法找到XCUIElement是否具有焦点? [英] Is there a way to find if the XCUIElement has focus or not?
问题描述
我的一个屏幕具有多个文本字段,我可以从其他不同的屏幕登陆到该屏幕.在每种情况下,我都将一个或另一个文本字段作为第一响应者.我无法编写测试来确定所需的textField是否具有焦点.
One of my screen has multiple text fields, I can land to this screen from different other screens. In each case I am making one or another text field as first responder. I am not able to write test to determine whether the desired textField has focus or not.
当我在控制台中打印文本字段时-
When I print the textfield in console -
输出:{ TextField 0x131171d40:特征:146031247360,关注,{{6.0,108.3},{402.0,35.0}},值: }
Output: { TextField 0x131171d40: traits: 146031247360, Focused, {{6.0, 108.3}, {402.0, 35.0}}, value: }
但是我在XCUIElement上找不到任何focus/isFocus属性.
But I could not find any focus/isFocus property on XCUIElement.
有没有办法做到这一点?
Is there a way to achieve this ?
推荐答案
我参加聚会有点晚了:)但是,据我从转储变量XCUIElement所看到的,它具有一个有趣的属性:
I little bit late for the party :) However as far as I can see from dumping the variable XCUIElement it has one interesting property:
属性名称:hasKeyboardFocus
property name: hasKeyboardFocus
属性类型:TB,R
因此,您可以通过以下方式检查元素是否具有焦点:
So you can check if your element has focus the following way:
let hasFocus = (yourTextField.value(forKey: "hasKeyboardFocus") as? Bool) ?? false
注意:您可以转储具有以下扩展名的任何NSObject子类的属性变量:
NB: you can dump the property variables of any NSObject sublass with following extension:
extension NSObject {
func dumpProperties() {
var outCount: UInt32 = 0
let properties = class_copyPropertyList(self.dynamicType, &outCount)
for index in 0...outCount {
let property = properties[Int(index)]
if nil == property {
continue
}
if let propertyName = String.fromCString(property_getName(property)) {
print("property name: \(propertyName)")
}
if let propertyType = String.fromCString(property_getAttributes(property)) {
print("property type: \(propertyType)")
}
}
}
}
更新:属性转储,Swift 4:*
extension NSObject {
func dumpProperties() {
var outCount: UInt32 = 0
let properties = class_copyPropertyList(type(of: self), &outCount)
for index in 0...outCount {
guard let property = properties?[Int(index)] else {
continue
}
let propertyName = String(cString: property_getName(property))
print("property name: \(propertyName)")
guard let propertyAttributes = property_getAttributes(property) else {
continue
}
let propertyType = String(cString: propertyAttributes)
print("property type: \(propertyType)")
}
}
}
这篇关于有没有办法找到XCUIElement是否具有焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!