18

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

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 it Commented Jan 8, 2016 at 19:45

4 Answers 4

33

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() 
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

I add this into the UI test code, but it does not work
here is my codeoverride func setUp() { super.setUp() NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow", name: UIKeyboardDidShowNotification, object: XCUIApplication()) }
Should we use NotificationCenter in a UI Test? I doubt it.
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 } 

2 Comments

could you modify your answer above to read private func... instead of private fund..."
XCUIApplication().keyboards.element(boundBy: 0).exists also works
0
extension XCUIApplication { var keyboard: XCUIElement? { let keyboard = keyboards.element(boundBy: 0) return (keyboard.exists && keyboard.isHittable) ? keyboard : nil } var isKeyboardVisible: Bool { keyboard != nil } var isKeyboardHidden: Bool { !isKeyboardVisible } func closeKeyboard() { guard let keyboard else { return } let hideKeyboardButton = keyboard.buttons["Hide keyboard"] if hideKeyboardButton.exists && hideKeyboardButton.isHittable { hideKeyboardButton.tap() } else { let firstKey = keys.element(boundBy: 0) if firstKey.exists { typeText("\n") } } } } 

1 Comment

Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, tailor your answers to the question. And answers should explain how it solves the issue. Code-only answers are not nearly as useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.