小编lei*_*zeQ的帖子

如何在UI测试期间在Swift应用程序中隐藏键盘

我刚开始在XCode 7中进行UI测试并遇到了这个问题:

我需要在文本字段中输入文本然后单击按钮.不幸的是,这个按钮隐藏在键盘后面,在将文本输入文本字段时出现.XCode正在尝试滚动以使其可见但我的视图不可滚动因此失败.

我目前的解决方案是:

let textField = app.textFields["placeholder"]
textField.tap()
textField.typeText("my text")
app.childrenMatchingType(.Window).elementBoundByIndex(0).tap() // hide keyboard
app.buttons["hidden button"].tap()
Run Code Online (Sandbox Code Playgroud)

我可以这样做,因为我的ViewController正在拦截触摸:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    view.endEditing(false)
    super.touchesBegan(touches, withEvent: event)
}
Run Code Online (Sandbox Code Playgroud)

我对我的解决方案并不满意,在UI测试期间有没有其他方法可以隐藏键盘?

testing user-interface ios swift xcode-ui-testing

20
推荐指数
7
解决办法
1万
查看次数

标签 统计

ios ×1

swift ×1

testing ×1

user-interface ×1

xcode-ui-testing ×1