How to simulate a soft keyboard click on IOS simulator?

时间:2016-10-19 17:39:46

标签: ios swift xctest iphone-softkeyboard

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 个答案:

答案 0 :(得分:0)

您需要使用键盘焦点调用typeText()元素,而不仅仅是app

如果元素是文本字段,您将找到文本字段元素并在其上调用typeText()

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

您需要将查询替换为您自己的查询,以查找具有键盘焦点的元素。通常,这将是UITextField的后代,但如果您使用的是自定义视图,则需要使用更自定义的查询。