5

I'm trying to understand how UIAutomation can be used to perform automatic application tests, so I've created a temperature converter app that can be driven from the JavaScript interface. Here's the UI:

enter image description here

The top text field has an Accessibility label of "Celsius", similarly the bottom field has "Fahrenheit" as its Accessibility label. I'm trying to drive it through this script:

UIALogger.logStart("Test -40ºC == -40ºF");
var window = UIATarget.localTarget().frontMostApp().mainWindow();
var celsiusField = window.textFields()["Celsius"];
var fahrenheitField = window.textFields()["Fahrenheit"];
var convertButton = window.buttons()["Convert"];

celsiusField.setValue("-40");
convertButton.tap();
var fahrenheitValue = fahrenheitField.value();
if (fahrenheitValue == "-40.0") {
    UIALogger.logPass("-40C == -40F");
} else {
    UIALogger.logFail("-40C == " + fahrenheitValue + "F");
}

This correctly sets the text in the Celsius field to "-40", and when the button is tapped, the app updates the text in the Fahrenheit field to "-40.0". However, fahrenheitValue has the value "Fahrenheit" (the name/label of the text field), so the test fails. I wondered whether this was maybe a timing issue, so put in a one-second delay after the tap, which didn't change the behaviour.

Is it possible to get the text out of the text field and compare it with the expected value?

3 Answers3

5

Is it getting "Fahrenheit" because that's the accessibility label or because it's the placeholder text?

It looks like value() might return the placeholder text. One workaround could be to modify the placeholder value once the user starts typing to set it to be the text the user entered and then reverting it to Fahrenheit when they delete? I assume that even when you call setValue it acts as it would when the UITextField delegate methods get called?

NeilInglis
  • 3,431
  • 4
  • 30
  • 31
  • 1
    That appears to have been the answer. I removed the placeholder text, and now things work as they ought. I should probably file a bug over that :) –  Nov 25 '11 at 11:25
0

You could add a label to your view, update it and read it out.

Salamatizm
  • 51
  • 3
  • How? I tried using a UILabel instead of a UITextField, and had the same result. –  Nov 25 '11 at 11:08
0

Try hooking up the Accessibility Inspector to show what UIAutomation "sees" in your app. It might be that it's reading some stale values because of the way you set the values in your app.

One way of refreshing the values is to send an update event:

UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
Claus Broch
  • 9,212
  • 2
  • 29
  • 38