24

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 -

Output: { TextField 0x131171d40: traits: 146031247360, Focused, {{6.0, 108.3}, {402.0, 35.0}}, value: ​ }

But I could not find any focus/isFocus property on XCUIElement.

Is there a way to achieve this ?

Sandy
  • 3,021
  • 1
  • 21
  • 29
  • XCUIElement seems to derive some of its properties from the accessibility traits, but does not necessarily expose the traits. Have you tried the `selected` property to see if that gives you what you want? – Charles A. Oct 01 '15 at 22:32
  • `selected` property is returning false/nil for my textFields. – Sandy Oct 01 '15 at 22:44
  • There is property `hasKeyboardFocus`, please see my answer bellow. – hris.to Mar 10 '16 at 11:51

5 Answers5

29

I little bit late for the party :) However as far as I can see from dumping the variable XCUIElement it has one interesting property:

property name: hasKeyboardFocus

property type: TB,R

So you can check if your element has focus the following way:

let hasFocus = (yourTextField.value(forKey: "hasKeyboardFocus") as? Bool) ?? false

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)")
            }
        }
    }
}

Update: Properties dump, 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)")
        }
    }
}
leanne
  • 7,940
  • 48
  • 77
hris.to
  • 6,235
  • 3
  • 46
  • 55
  • 1
    Fantastic find, thanks for responding with this answer! – Patrick Apr 10 '16 at 09:51
  • 3
    brilliant answer! if you like to wait for element with focus just use predicate and expectation `let focusPredicate = NSPredicate(format: "exists == true && hasKeyboardFocus == true")` – user3292998 Feb 20 '17 at 13:00
  • 1
    Excellent find!! Apple's documentation states "The XCUIElementAttributes protocol, as adopted by XCUIElement, provides additional properties for querying the current state of a UI element's attributes." That protocol has a 'hasFocus' variable, however when trying to actually use this, you get the "XCUIElement has no member 'hasFocus'" message from xcode. Thanks again! – Jacob Boyd Feb 21 '18 at 17:56
  • 1
    @hris.to How did you find the property named "hasKeyboardFocus"? Even Apple's documentation does not shows about it. Can you optimize the above code to Swift 4 and above? It throws the error on the line "self.dynamicType" – Confused Jul 23 '18 at 11:22
  • @leanne thanks a lot for the edit! One doubt. What is the meaning for property types? For example, what is the meaning of "TB,R"? – Confused Aug 05 '18 at 21:16
  • @Confused: If you option-click on `property_getAttributes` in Xcode, the Quick Help says "The format of the attribute string is described in [Declared Properties](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101) in [Objective-C Runtime Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008048)." – leanne Aug 06 '18 at 00:17
  • @Confused: Those references show that a type starts with `T`, then is followed by an @encode type ([`B` apparently means "A C++ bool or a C99 _Bool"](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1). The `R`, shown in [Table 7-1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW6), under *Property Type String*, apparently means `read-only`. Interesting... – leanne Aug 06 '18 at 00:26
13

Based on @hris.to's excellent answer, I put together this little extension (works in Swift 4 as well)...

extension XCUIElement
{
    func hasFocus() -> Bool {
        let hasKeyboardFocus = (self.value(forKey: "hasKeyboardFocus") as? Bool) ?? false
        return hasKeyboardFocus
    }
}
leanne
  • 7,940
  • 48
  • 77
0

I don't know but maybe selected property on XCUIElement is what you're looking for. Try it.

Arsen
  • 10,815
  • 2
  • 34
  • 46
0

There is a method on NSObject via an extension (from UIAccessibility.h), which is available on XCUIElement named accessibilityElementIsFocused(), but it seems to not return true even when the debugDescription of that element clearly says Focused. There are a few other related methods accessibilityElementDidLoseFocus() and accessibilityElementDidBecomeFocused(), which appear to be methods intended to be overridden by a subclasses of NSObject to get notified of changes to the Focused state.

After this digging I'm inclined to say that the notion of Focused that we are discussing is not the same as the firstResponder state, which is probably what you're hoping to know. I believe at this point that focus indicates that the element is the focus of some assistive technology like VoiceOver, based on some of the comments for these methods.

If you want to be able to tell if an item is firstResponder directly, I think at this point it's time to file a bug report.

If there is a way to interact directly with the software keyboard, a hacky workaround might be to get an XCUIElement for your UITextField and compare the value of your UITextField element before and after typing with the keyboard (you may want to type a character then a backspace). Having said that, I could not figure out how to get the keyboard directly in a cursory attempt.

Charles A.
  • 10,685
  • 1
  • 42
  • 39
0

I have observed some instances where

XCUIApplication().textFields[{index/identifier}].selected

does not return true even when I had my cursor focussed on the text field but I was able to enter text with .typeText(). However, .enabled correctly returns true , It appears 'enabled' value represents that UI element has accessibility enabled and you can interact with it.

Not a clean solution but you can try,

XCUIApplication().textFields.elementMatchingPredicate("predicate")

or .elementMatchingType() to get XCUIElement and write your test block while handling the exception condition with guard statement to throw appropriate error if the textField is not found.

Sushant
  • 440
  • 3
  • 8
  • 2
    `enable` will not help here. Enable tells - Whether or not the element is enabled for user interaction. None of my textField is in disabled state. Did you get my question ? I was asking about how can I validate if my textField is first responder. – Sandy Oct 02 '15 at 18:14
  • copy that.From your question, I read your objective was to determine if the Textfield has focus or not so that you can interact with it.In case you want to verify the current first responder from the UIView is your expected UITextField element, you can check this gist link (https://gist.github.com/steipete/8737196). A hacky workaround could be to get the first responder UIView title to match against your expected textField title and write your conditional test. – Sushant Oct 03 '15 at 18:11