how to Detect if Keyboard is shown in Xcode UI test
Asked Answered
Z

3

17

I am writing a UI text in swift under the new Xcode 7 UI test framework. the requirement is to test whether the system keyboard is shown in an app. can someone give me a clue on how to do that? thanks

Zeena answered 8/1, 2016 at 19:43 Comment(1)
BTW, when I was trying to look for the accessibility in the debug mode of the keyboard, I can't not see it in the app. so I guess that since it is a system keyboard, so we can not see itZeena
C
1

Add two observers

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")
}

Whenever the keyboard is visible keyboardVisible will be called and whenever the keyboard is hidden keyboardHidden will be called.

Collinsworth answered 8/1, 2016 at 23:13 Comment(3)
I add this into the UI test code, but it does not workZeena
here is my codeoverride func setUp() { super.setUp() NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow", name: UIKeyboardDidShowNotification, object: XCUIApplication()) }Zeena
Should we use NotificationCenter in a UI Test? I doubt it.Emmanuelemmeline
U
32

Try this check:

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

Or check for specific keyboard keys like:

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

You can also control interactions on the keyboard:

let app = XCUIApplication()
app.keyboards.buttons["Next:"].tap()
Uniformize answered 4/5, 2016 at 9:32 Comment(0)
C
1

Add two observers

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")
}

Whenever the keyboard is visible keyboardVisible will be called and whenever the keyboard is hidden keyboardHidden will be called.

Collinsworth answered 8/1, 2016 at 23:13 Comment(3)
I add this into the UI test code, but it does not workZeena
here is my codeoverride func setUp() { super.setUp() NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow", name: UIKeyboardDidShowNotification, object: XCUIApplication()) }Zeena
Should we use NotificationCenter in a UI Test? I doubt it.Emmanuelemmeline
S
0

I found the keyboard count check didnt work on one of my apps (it returned a count of 1 even when the keyboard was hidden), so amended it slightly:

private func isKeyboardShown() -> Bool {
    return XCUIApplication().keyboards.keys.count > 0
}
Stoltz answered 6/5, 2020 at 13:53 Comment(2)
could you modify your answer above to read private func... instead of private fund..."Picador
XCUIApplication().keyboards.element(boundBy: 0).exists also worksRaychel

© 2022 - 2024 — McMap. All rights reserved.