1

I'm having trouble getting interface elements by name in a UIAutomation script.

I have set up the accessibility panel for a text control:

accessibility panel

And I know that I have the right parent view, as this code will work to set the field contents:

var view = UIATarget.localTarget().frontMostApp().mainWindow();
var textfields = view.textFields();
textfields[0].setValue("testuser");

Unfortunately, if I try to access the field by name, as the docs seem to indicate I should be able to do, I get an error:

var view = UIATarget.localTarget().frontMostApp().mainWindow();
var textfields = view.textFields();
textfields['foo'].setValue("testuser");

Cannot perform action on invalid element: UIAElementNil from target.frontMostApp().mainWindow().textFields()["foo"]

Does anybody have any idea why the find-by-name functionality doesn't seem to be working here for me?

Thanks for any insight!

Sean McMains
  • 57,907
  • 13
  • 47
  • 54

3 Answers3

3

If you are working with the accessibility Layer, you must to enable it in the Device/Simulator:

Settings-> General-> Accessibility-> Accessibility Inspector-> ON

Stephan
  • 41,764
  • 65
  • 238
  • 329
edavidfs
  • 31
  • 2
1

It appears that UI Automation looks for the element's name in the Title field rather than the documented Accessibility Label field; at least, that's my experience. Adding a Title to your UI elements (via Attributes) should result in the desired behaviour.

Update: It now appears, after further experimentation, that the field to edit may be the Accessibility Identifier (not Label), which AFAICT can only be set programmatically.

0

According to UI Automation Reference Guide

UIAElementArray section, you may want to try like this:

var textfields = view.textFields();

textfields.withName("foo")[0].setValue("testuser");
Tuyen Nguyen
  • 4,389
  • 7
  • 51
  • 77