Search code examples
javascriptiosxcode4ios-ui-automation

Can't get value of UIAStaticText?


I'm trying to access the value() of a UIAStaticText (a UILabel in the objective C code) JavaScript object. I can set the value just fine using setValue() and I can confirm that the simulator updates the text, but when I try to get the value I only get null.

i.e.

text.setValue("new text");

updates the label to "new text" in the simulator.

text.value()

still returns null after I've set the value.

What am I doing wrong here?


Solution

  • Looks like the root of the problem is in accessibilityValue property of UILabel returning accessibilityLabel, not the UILabel text as it should. To solve this problem I override accessibilityValue property in my custom UILabel category.

    @interface UILabel (MyAccessibility)
    @property(nonatomic, copy) NSString *accessibilityValue;
    @end
    
    
    @implementation UILabel (MyAccessibility)
    
    @dynamic accessibilityValue;
    
    -(NSString *)accessibilityValue {
        // Here we force UIKit to return Label value, not the accessibility label 
        return self.text;
    }
    
    @end
    

    Besides this minor trick, I always try to prefer accessibility Identifier over accessibility Label to prepare my UI elements for automated testing. Check out "Accessibility Label and Identifier Attributes" section of Instruments New Features User Guide.