How to determine if a keyboard is displayed in an Xcode UI test

I am writing user interface text in swift in the new Xcode 7 UI test environment. you need to check if the system keyboard is displayed in the application. can anyone let me know how to do this? thanks

+6
source share
2 answers

Add two watchers

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.

0
source

Try this check:

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

Or check for specific keyboard keys, for example:

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

You can also control keyboard interaction:

 let app = XCUIApplication() app.keyboards.buttons["Next:"].tap() 
+11
source

All Articles