17

我正在新的 Xcode 7 UI 测试框架下快速编写 UI 文本。要求是测试系统键盘是否显示在应用程序中。有人可以告诉我如何做到这一点吗?谢谢

4

3 回答 3

30

试试这个检查:

let app = XCUIApplication()
XCTAssert(app.keyboards.count > 0, "The keyboard is not shown")

或检查特定的键盘键,例如:

let app = XCUIApplication()
XCTAssert(app.keyboards.buttons["Next:"].exists, "The keyboard has no Next button")

您还可以控制键盘上的交互:

let app = XCUIApplication()
app.keyboards.buttons["Next:"].tap()
于 2016-05-04T09:32:38.230 回答
1

添加两个观察者

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardVisible:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHidden:", name: UIKeyboardDidHideNotification, object: nil)

func keyboardVisible(notif: NSNotification) {
    print("keyboardVisible")
}

func keyboardHidden(notif: NSNotification) {
    print("keyboardHidden")
}

只要键盘可见keyboardVisible就会被调用,只要键盘被隐藏keyboardHidden就会被调用。

于 2016-01-08T23:13:46.623 回答
0

我发现键盘计数检查在我的一个应用程序上不起作用(即使键盘被隐藏,它也返回计数 1),所以稍微修改了一下:

private func isKeyboardShown() -> Bool {
    return XCUIApplication().keyboards.keys.count > 0
}
于 2020-05-06T13:53:30.633 回答