How to simulate a soft keyboard click on IOS simulator?

743 Views Asked by At

I am using XCTest to test my IOS app. As part of a test case I need to enter a number into a field (which is not a text box) When I record the test, following code is generated when I use the soft keyboard on the simulator(IPAD/IPhone)

app.staticTexts["0"].tap() //Line 1
app.typeText("23")         //Line 2

When I execute the test, the soft keyboard pops up after Line 1. But when Line 2 is executed, following error appears

UI Testing Failure - Neither element nor any descendant has keyboard focus

My app requires to be installed on IPads/IPhones. So I need to make it run through the soft keyboard route only.

So I think typeText is not the correct method. What is the method to simulate clicks on a soft/virtual keyboard in an IOS simulator?

1

There are 1 best solutions below

0
Oletha On

You need to call typeText() on the element with keyboard focus, not just app.

If the element was a text field, you would find the text field element and call typeText() on that.

let element = app.textFields["myTextField"]
element.typeText("23")

You will need to replace the query with your own one for finding the element which has keyboard focus. Usually, this would be a descendant of UITextField but you will need to use a more custom query if you are using a custom view instead.