Clicking keyboard 'Next' key using UIAutomation
Asked Answered
L

5

13

I have a search field in my app and I have set the return key type of the keyboard for this field to UIReturnKeyNext. I am attempting to write a UIAutomation test that clicks the Next button on the keyboard using the following line:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().keys().firstWithName("next");

This call is failing because the key with name 'next' is not being found. I have done a dump of all of the elements in my app using:

UIATarget.localTarget().frontMostApp().logElementTree();

This reveals that there is indeed a key in the keyboard with name 'next', but somehow my attempt to retrieve it as show above still fails. I can however retrieve other keys (like the key for the letter 'u') using this method. Is there a known issue here or am I doing something wrong?

I've tried other variations with no luck:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().elements()["next"];

Here is a screen capture of the elements in my UIAKeyboard:

return key dump

Lenticular answered 5/1, 2012 at 20:25 Comment(0)
L
2

The following works for me:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWi‌​thPredicate("name contains[c] 'next'"); 
Lenticular answered 8/1, 2012 at 23:23 Comment(0)
S
8

If you just want to click it, and you know the keyboard has "next" as "Return key" (defined in your nib), then you can use this:

app.keyboard().typeString("\n");
Strasser answered 19/3, 2013 at 10:52 Comment(2)
Genius.........Lotti
Nowadays this does not work, but you can call typeText("\n") on your desired textField to achieve the same resultWilletta
B
8

Jelle's approach worked for me. But I also found an alternative way if anybody needed it.

XCUIApplication().keyboards.buttons["return"].tap()

Where you can create XCUIApplication() as a singleton on each UI Test session. The thing about this approach is you can now distinguish between return and done and other variants and even check for their existence.

You can go extra and do something like following:

extension UIReturnKeyType {
    public var title: String {
        switch self {
        case .next:
            return "Next"
        case .default:
            return "return"
        case .continue:
            return "Continue"
        case .done:
            return "Done"
        case .emergencyCall:
            return "Emergency call"
        case .go:
            return "Go"
        case .join:
            return "Join"
        case .route:
            return "Route"
        case .yahoo, .google, .search:
            return "Search"
        case .send:
            return "Send"
        }
    }
}

extension XCUIElement {
    func tap(button: UIReturnKeyType) {
        XCUIApplication().keyboards.buttons[button.title].tap()
    }
}

And you can use it like:

let usernameTextField = XCUIApplication().textFields["username"]
usernameTextField.typeText("username")
usernameTextField.tap(button: .next)
Bubb answered 27/8, 2018 at 21:16 Comment(2)
'title' is not a property of 'UIReturnKeyType'.Footworn
@ThEuSeFuL It is, it's added in the code sample above as an extension to itBubb
K
2

I dont't have an example to test, but as the "Next" button is an UIAButton, and not an UIAKey you could try :

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons()["next"];

If it doesn't work, you can also try

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons()[4];
Kitchenware answered 8/1, 2012 at 14:39 Comment(2)
I tried both and had no luck. Thanks for the suggestions though.Lenticular
Julien, your answer prompted me to play around with this again, and using the buttons() function did lead to a working solution. Here is what works for me: UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWithPredicate("name contains[c] 'next'"); It seems to me that buttons()["next"] should have worked and I don't know why it didn't, but the above does work. Thanks for the suggestion.Lenticular
L
2

The following works for me:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWi‌​thPredicate("name contains[c] 'next'"); 
Lenticular answered 8/1, 2012 at 23:23 Comment(0)
M
0

For me, keyboard does not fall under mainWindow() in the View Hierarchy. It is at the same level as mainWindow() when you logElementTree() from top level. So, what you want to do is:

UIATarget.localTarget().frontMostApp().keyboard().buttons()["next"];

This worked for me when I was trying to press the "Search" button on keyboard.

Madelaine answered 4/2, 2015 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.