iOS Instruments Automation "setValue" not working
Asked Answered
S

2

6

I just want to test my iOS UI with UI Automation but I got stuck when it comes to enter text into UITextFields. The documentation says that the method "setValue(...)" should do the trick but it doesn't.

I always get this error:

Script threw an uncaught JavaScript error: Unexpected error in -[UIATextField_0x9952690 setValue:], /SourceCache/UIAutomation_Sim/UIAutomation-271/Framework/UIAElement.m line 1142, kAXErrorSuccess on line 15 of login.js, #0 setValue() 

The code looks like this:

var textfields = UIATarget.localTarget().frontMostApp().mainWindow().textFields();
username = textfields["username"];
username.setValue("test");

The username field is not null or undefined.

My second solution was this JS project: https://github.com/alexvollmer/tuneup_js#readme It has a "typeString" method for text fields but it is a little bit buggy and fails when it comes to enter numbers and capital letters.

I'm working with iOS6.1, Instruments Version 4.6 (46000), Xcode Version 4.6 (4H127).

Any help is appreciated!

Skyeskyhigh answered 13/2, 2013 at 15:43 Comment(2)
Hmm. I'm able to set the value of text fields in my app with UI Automation just fine. Your automation code looks correct. I suspect something is wrong in the Objective C world. Are you using a custom subclass of UITextField? Can you build a dummy app with a single text field and try to set the value of that field on your machine? That might help track down the problem.Codd
In other projects the "setValue()" function works. I think the problem is caused by an unfinished animation. when the user taps the text field the underlying view scrolls to the top to be visible when the keyboard shows up. "target.delay(1)" does what I was looking for and now the "setValue()" function works.Skyeskyhigh
I
10

Check if below works for you,

target.delay(1); UIATarget.localTarget().frontMostApp().mainWindow().textFields()["username"].tap(); target.delay(1); UIATarget.localTarget().frontMostApp().mainWindow().textFields()["username"].setValue("test");

Check for correct hierarchy if it does not work for you..

Intent answered 15/2, 2013 at 7:45 Comment(1)
It seems that "target.delay(1)" did the trick. Your code works fine for me, thank you!Skyeskyhigh
S
0

I came here struggling to understand how UI Automation scripting works, as the documentation and example code out there is really lacking (or written from a very specific point of view that doesn't explain all cases).

Here are two things on the subject I learned:

  1. First and foremost, you must enable Accessibility on the element. I had disabled it in my trials and things were behaving very strangely until I re-enabled it (getting a similar "unexpected error" as you). So make sure you have Accessibility turned on. But note that some things still work with it disabled. For example, you can still look up the element by its name (accessibilityIdentifier), but you won't be able to tap it or call setValue().

enter image description here

  1. You have to dig through your application hierarchy. My assumption was that giving an element an accessibilityIdentifier would allow me to simply look up the element with that identifier like HTML's getElementById. But it is not like this at all. You have to dig through your hierarchy level by level, even when using an accessibilityIdentifier. Luckily you can use UIAElement.logElementTree() to get some clues as to the correct hierarchy to use.

So far I am NOT at all impressed with this framework, but I am ever so slowly making progress.

Speedwell answered 29/10, 2015 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.