Xcode UI Test UIKeyInput typeText
Asked Answered
E

2

5

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.

Elyseelysee answered 2/3, 2016 at 6:32 Comment(4)
forums.developer.apple.com/message/29549#29549Pyretic
btw, try to tap before typing. and check that simulator settings: Hardware - keyboard - connect hardware keyboard (false)Pyretic
I tried both of those things. I also tried Simulator > Reset Contents and Settings... Still not working.Elyseelysee
I answered in the question you linked: https://mcmap.net/q/143143/-ui-testing-failure-neither-element-nor-any-descendant-has-keyboard-focus-on-securetextfieldWillianwillie
T
6

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")
Tommyetommyrot answered 18/2, 2017 at 12:39 Comment(1)
This works as intended, but it the test will fail if the simulator keyboard is down. It is also nice to add little timeout before tapping, as it has a tendency of failing with an invalid number of keystrokes every now and then...Megalith
M
2

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".

Mendiola answered 2/3, 2016 at 11:13 Comment(3)
I'll try that, but I don't think that will solve the issue as I don't need to click it during recording since I call codeInputView.becomeForstResponder in viewDidLoad.Elyseelysee
First responder happens asynchronously so The framework might not have registered the change yet.Mendiola
I tried it, but no luck. Note: CodeInputView isn't a UITextField. It's a UIView that conforms to UIKeyInput.Elyseelysee

© 2022 - 2024 — McMap. All rights reserved.