I am trying to use UIAutomation in Instruments with the iPhone Simulator and the latest iOS SDK 4.1. Here is the snippet of javascript that is having a problem:
// there are sufficient delays here to make sure the view is loaded
UIATarget.localTarget().frontMostApp().logElementTree();
main.buttons()["theButton"].tap();
UIALogger.logMessage("The button tapped");
for (var b = 0; b < main.buttons().length; b++)
{
UIALogger.logMessage("Button title: " + main.buttons()[b].name());
}
main.toolbar().buttons()["OK"].tap();
UIALogger.logMessage("OK tapped");
The button name of "theButton" shows up in the logElementTree as well as showing up when I am logging the names of all the buttons, so it is correctly configured in the Interface Builder, but for some reason, it does not get tapped. I have other buttons earlier in the script that are getting tapped just fine, and if I abort the script at the point of the non-tapped button, I can click on the button in the Simulator and it works as expected.
EDIT: In the javascript for loop shown above, I had it tap each of the buttons in the main.buttons() array, and only 1 of the 12 identical buttons on the view gets the tap.
Also, in case you were wondering, I have this code at the top of the javascript file:
var target = UIATarget.localTarget();
var app = target.frontMostApp();
var main = app.mainWindow();
And here is the line that shows the button information from the sequence of entries that logElementTree puts into the log messages:
4) UIAButton [name:theButton value:(null) NSRect: {{25, 93}, {74, 74}}]
I had similar problem with tapping a button on an actionSheet. I'm performing the scripts on the actual device. My code is:
//check that action sheet is on the screen
app.actionSheetIsValid();
//check that the button is on the actionSheet
actionSheet.actionSheetButton("Exit");
//touch Exit button
actionSheet.tapActionSheetButtonByName("Exit");
All functions perform and pass, however the button is not pressed.
logElementTree();
shows that the button is there.
I tried to add target.pushTimeout(5);
after I check that the button is on the actionSheet in order to give it some time to detect and tap the button. This didn't help.
Then I added: target.delay(1);
right after I check that the button is on the actionSheet and before tapping it.
It helped in my case, the script is more robust and stable now.