Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking keyboard 'Next' key using UIAutomation

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

like image 400
kodie Avatar asked Jan 05 '12 20:01

kodie


2 Answers

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");
like image 148
Jelle Avatar answered Oct 28 '22 16:10

Jelle


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)
like image 44
manman Avatar answered Oct 28 '22 15:10

manman