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:
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?
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?
You could add a label to your view, update it and read it out.
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);
Related
I am new to swift programming and Xcode
However I am creating a dart counter app but having trouble connecting the buttons to Text fields
I have buttons numbered 0-20 on the main story board and three text fields (one for each dart) how do I connect the buttons to the UITextFields - I can’t figure this out
Thanks
Based on my understanding of your issue, you actually need to connect the buttons to the text field in code. There is not going to be a simple way to do this in the interface builder. What you are looking for is to have some action happen when the user presses a number, and this may be added to the current value in the text field.
Create a function that handles any number button being pressed.
#IBAction func buttonPress(sender: UIButton) {
print(sender.tag)
//TODO: process the button based on its tag
}
Update all of the buttons in interface builder to have a tag value according to their number. This will let you handle them all in the same function (above) and differentiate between them.
Update the TODO block to handle the action appropriately. I would start by adding a variable to keep track of which dart number you are on, and using a switch statement to handle putting the value in the appropriate dart text box.
So there is always a current text field. Maintain an instance property that points to it. When the user taps a number, use that instance property to enter that value in that text field. When the user taps Enter, change the instance property to point to the next text field.
I am making a game in unity3d for iPad only.
In one screen I need to take text input from user and for that I use Input Field.
I need to make some UI change when user open keyboard by taping Input Field. For that I need keyboard open(show) event. Input Field give me just two event 1. Text Change 2. Lost Focus. But I need get Focus event.
I read official document but can't find any relevant event. I search on internet for event, I get keystroke event but I need keyboard open event before user start typing.
In native iOS app we use UITextFieldDelegate Method - (void)textFieldDidBeginEditing:(UITextField *)textFieldor UIKeyboardDidShowNotification. Is there any way in Unity3d to get this kind of behaviour.
For input field with the new UI there is an isFocused variable.
http://docs.unity3d.com/ScriptReference/UI.InputField.html
http://docs.unity3d.com/ScriptReference/UI.InputField-isFocused.html
#pragma strict
// Required when Using UI elements.
public class Example {
public var mainInputField;
function Update() {
//If the input field is focused, change its color to green.
if (mainInputField.GetComponent.<InputField>().isFocused == true) {
mainInputField.GetComponent.<Image>().color = Color.green;
}
}
}
So if your field gets focused and it returns true you can execute some code. That is, if I understand you correctly.
I can't find anything that lets you know the InputField has been selected but a workaround may be setting the Interactable property to false and having an 'Edit' button near the field. When the 'Edit' button is pressed, you can change the UI elements and set the InputField.interactable = true.
I have an input field the user needs to fill with an alphanumeric code. The keyboard the user uses to type the code has a dynamic return button that changes to "send" as he writes some text on the field. When the field is empty the return button has the default value.
To dynamically change the return button type I use the following code:
if([textField.text isEqualToString:#""])
{
textField.returnKeyType = UIReturnKeyDefault;
}
else
{
textField.returnKeyType = UIReturnKeySend;
}
[textField reloadInputViews];
However this has the following drawback: since the code is alphanumeric, the user may be typing numbers, and yet the keyboard will always switch back to the letter keyboard, so to type more than one number in a row he will need to be continuously switching to number keyboard.
Is there any way to dynamically change the return key of a keyboard as the user types but to preserve the keyboard state to letters or numbers keyboard?
I think this is not a bug on Apple's side, more a missing implementation of API for the keyboard. With the new iOS8 API you might want to create your own keyboard returning the UIKeyboardType.
For iOS7 I worked around by inspecting the views of the keyboard. Use the US2KeyboardType CocoaPod or the source:
https://github.com/ustwo/US2KeyboardType
As Martin noted above, it's not a bug on Apple's side, but on my side. However I'll be posting the solution I've found since it is the one that solves that particular problem:
Instead of manually changing the return key type when there is text on the text field, Apple provides us with a property called enablesReturnKeyAutomatically that when set to YES it automatically manages the issue, enabling and disabling the return key depending on whether there is text or not in the text field.
Therefore you don't need to modify the returnKeyType property, and thus, no calling to reloadInputViews is required, so the keyboard doesn't change back to its original state.
I have a piece of code that's reacting to a text field being typed in. I want to check when the .text of the field is empty (either backspaced all the way or cleared with the little round X button on the right).
If I check for .text == #"" my code works provided the user backspaced. It does not work if the user used the clear button.
What does the clear button set the text field's .text to? (I've logged it out and it looks blank to me if I print it between two other characters.)
Perhaps I need to check for more than just .text == #""? Perhaps .length == 0 or something else?
Thanks!
The "clear" button probably sets the text property to nil.
The best check whenever you wish to see if text is empty is to use:
if (someText.length == 0) {
// soneText has no text (or someText is nil)
}
This works for empty strings as well as nil values.
I have a view controller that controls 2 text fields and an array that displays in a table. How would I go about keeping a button disabled until the 2 fields have at least one character and the array is not empty. I am thinking about using cocoa bindings, however I can't seem to figure out a solution.
Currently my button is binded to
BOOL buttonIsEnabled;
I use that in a notification function in order to keep the button disabled, except the button will only reenable if I call that notification function.
-(void)controlTextDidChange
This means if i make a change in an array, the button wont reenable until I re-enter text. I can't seem to figure out an alternative solution. Any suggestions? Thank you.
One solution:
bind the two text fields two two strings (say text1 and text2)
in the text field bindings check Continuously Updates Value
Add this code:
- (BOOL)buttonIsEnabled {
return (self.text1.length>0 && self.text2.length>0);
}
+ (NSSet *)keyPathsForValuesAffectingButtonIsEnabled {
return [NSSet setWithObjects:#"text1",#"text2",nil];
}
Lastly bind the buttons enabled binding to buttonIsEnabled.
Because of the Continuously Updates Value text1 or text2 will change whenever characters are added to or removed from the text fields.
And the + (NSSet *)keyPathsForValuesAffectingButtonIsEnabled method will cause a Key-Value change notification to be posted for buttonIsEnabled whenever text1 or text2 change.
You can create a custom cell, give outlets to your 2 textfields and button.
Now on cell for row at index path after filling the textfields text values, put a condition checking the length of the textfields. If length is greater than 0 you can enable the button or keep it disable.
Along with this you need to put same code in delegate method (textFieldDidChange) of textfield. So that when ever new text is entered the button gets enable or disabled.