Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type into a UITextView inside a XCTestCase

How do you type into a UITextView inside a XCTestCase? The UITextView is the first responder so it already had focus and the keyboard is up.

I've tried:

app.typeText("footer")
app.textViews.element(boundBy: 0).typeText("foobar")
app.links.element(boundBy: 0).typeText("foobar")

for some reason app.textViews.count always returns 0?

like image 347
Mystic Avatar asked Jun 02 '17 18:06

Mystic


1 Answers

The problem is not that .typeText(_:) doesn't work, it's that your query doesn't resolve to an element.

It can sometimes seem like the query is not working properly when the view you're trying to find is inside an accessible container view of some sort. You can mitigate this by explicitly disabling accessibility in the container view.

Set the stack view that contains the text view to not be an accessibility element and then set an accessibility identifier on the text view.

// app code
let stack: UIStackView!
let textView: UITextView!
stack.isAccessibilityElement = false
textView.isAccessibilityElement = true
textView.accessibilityIdentifier = "myTextView"

// test code
let app = XCUIApplication()
let textView = app.textViews["myTextView"]
textView.typeText("something")
like image 52
Oletha Avatar answered Sep 19 '22 11:09

Oletha