Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use XCUIKeyboardKey constants?

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

like image 396
kbpontius Avatar asked Jul 29 '15 22:07

kbpontius


2 Answers

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)
like image 178
tsafrir Avatar answered Nov 11 '22 13:11

tsafrir


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.

like image 43
Samuël Avatar answered Nov 11 '22 14:11

Samuël