12

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

Himanshu
  • 31,810
  • 31
  • 111
  • 133
kodie
  • 429
  • 6
  • 13

5 Answers5

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");
Jelle
  • 1,024
  • 10
  • 18
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)
manman
  • 4,743
  • 3
  • 30
  • 42
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];
Julien
  • 9,312
  • 10
  • 63
  • 86
  • I tried both and had no luck. Thanks for the suggestions though. – kodie Jan 08 '12 at 23:10
  • 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. – kodie Jan 08 '12 at 23:21
2

The following works for me:

UIATarget.localTarget().frontMostApp().mainWindow().keyboard().buttons().firstWi‌​thPredicate("name contains[c] 'next'"); 
Dan
  • 10,531
  • 2
  • 36
  • 55
kodie
  • 429
  • 6
  • 13
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.

CptBugs
  • 21
  • 1
  • 4