我刚开始在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测试期间有没有其他方法可以隐藏键盘?