Search code examples
iphoneiosaccessibilityios-ui-automation

iOS - UI Automation get textfield by accessibility label?


So in my nib file i enabled accessibility and set the accessibility label of a textfield to "txt" I am trying to find this textfiled based on accessibility name and change its text.

var target = UIATarget.localTarget();
var application = target.frontMostApp(); 
var mainWindow = application.mainWindow();
mainWindow.logElementTree();

// This works
var textField = mainWindow.textFields()[0];

//this doesn't work
var textField = mainWindow.textFields()["txt"];

textField.setValue("Hello");

Am I doing anything wrong? How can I find a label based on accessibility label?

EDIT:

I am open to other solutions as well, I am trying to avoid getting the textfield based on the index


Solution

  • I have a similar problem and setting the accessibility label value in the code instead in the nib file solve my problem. For example, I have a UITextField that I need to access in my UIAutomation script, I would have to set the accessibility label value in the viewDidLoad method as shown below.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        testTxtField.accessibilityLabel = @"myTxtBox";
    }
    

    and below is my UIAutomation Script

    var target = UIATarget.localTarget();
    var application = target.frontMostApp(); 
    var mainWindow = application.mainWindow();
    mainWindow.logElementTree();
    
    // This works
    //var textField = mainWindow.textFields()[0];
    
    // Now, this work too.
    var textField = mainWindow.textFields()["myTxtBox"];
    textField.setValue("Hello");
    
    UIALogger.logMessage("Text field:" + textField.label());