0

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?

  • When `app.staticTexts["0"].tap()` is executed, the soft keyboard pops up. There is no soft keyboard before this line is executed. Next, if I introduce a breakpoint at Line 2 and intervene manually to click on the soft keyboard, I see the value being entered. That is why I ask if there is a way to simulate a soft keyboard click. – user2697620 Oct 19 '16 at 18:21

1 Answers1

0

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.

Oletha
  • 7,324
  • 1
  • 26
  • 46