How to use XCUIKeyboardKey constants?
Asked Answered
G

2

6

Just below the XCUIElementQuery class inside XCTest there are many constants where the documentation note above it states:

    Constants for use with -[XCUIElement typeKey:modifierFlags:],
    representing keys that have no textual representation. These comprise
    the set of control, function, and modifier keys found on most keyboards.

Apparently, it seems there should be a XCUIElement method called typeKey:modifierFlags:, as the note states. However, I can't seem to find this method anywhere in the documentation. I also don't see any method that substitutes this behavior, utilizing the aforementioned constants. Here is a partial list of the constants I'd be interested in using:

let XCUIKeyboardKeyDelete: String
let XCUIKeyboardKeyReturn: String
let XCUIKeyboardKeyTab: String
let XCUIKeyboardKeyCommand: String

Is this code just premature and will likely be completed later, as part of future Xcode 7 releases?

Ultimately, I'd like to be able to type cmd+a then use XCUIKeyboardKeyDelete to delete the contents of the given XCUIElement. If there are good alternatives currently available in Xcode 7 UI Testing, I'd love to learn know.

--> Swift 2.0 beta 4

Guyot answered 29/7, 2015 at 22:5 Comment(5)
I think [XCUIElement typeKey:modifierFlags:] got removed by accident or something on beta 4. The recorder uses it but it doesn't compile.Gunpowder
Good thought. I also have beta 3 installed, however, and I can't seem to find it there either. Any other ideas?Guyot
Nope... I submitted a radar.Gunpowder
Good idea, I'd definitely like to have that functionality. Hopefully they decide to implement it.Guyot
I also tried performSelector: on it and it doesn't seem to exist.Gunpowder
H
7

You can use XCUIElement.typeText(text: String) with XCUIKeyboardKeyDelete.

Example, when a text field is not empty:

textField.tapWithNumberOfTaps(2, numberOfTouches: 1)
app.menuItems["Select All"].tap()
textField.typeText(XCUIKeyboardKeyDelete)
Hannah answered 19/9, 2015 at 21:56 Comment(1)
This is a great idea and almost worked for me. tapWithNumberOfTaps only selected 1 character and XCUIKeyboardKeyDelete added a special character that didn't leave the field empty. Good stuff thoughCuthbert
O
2

It states in the documentation that the method [XCUIElement typeKey:modifierFlags:] is available on macOS only. See https://developer.apple.com/reference/xctest/xcuielement

I tested this out on Xcode 8.2.1 in a dummy macOS project UI Testing target, and indeed the method is existing. It does not exist on iOS.

Otiliaotina answered 24/2, 2017 at 11:39 Comment(1)
This is stated to be available on iPad OS now too, but despite my best efforts it does not work.Gowan

© 2022 - 2024 — McMap. All rights reserved.