Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode UI Test UIKeyInput typeText

During Xcode UI Testing, how do I insertText for a UIView that conforms to UIKeyInput?

I created a CodeInputView that conforms to UIKeyInput.

When I record myself manually entering a code, Xcode writes app.typeText("1234").

But, when I try to play that back, I get the error UI Testing Failure - Neither element nor any descendant has keyboard focus. And, none of the solutions to that question has worked.

like image 642
ma11hew28 Avatar asked Mar 02 '16 06:03

ma11hew28


2 Answers

I've found solution for UIKeyInput. Please, use:

app.keys["1"].tap()
app.keys["2"].tap()
app.keys["3"].tap()
app.keys["4"].tap()

// Instead of app.typeText("1234")
like image 158
fiveze Avatar answered Nov 11 '22 14:11

fiveze


The CodeInputView needs focus before typing text will work. Access the input by either its placeholder text or set the accessibility label manually.

let app = XCUIApplication()
let codeTextField = app.textFields["Your code"]

codeTextField.tap()
app.typeText("1234")

The above assumes the placeholder or accessibility label was set to "Your code".

like image 2
Joe Masilotti Avatar answered Nov 11 '22 13:11

Joe Masilotti