Sunday, May 29, 2016

How to check if the android keyboard is open in a UiAutomator test

How to check if the android keyboard is open in a UiAutomator test Ive been an Android SDET for almost two years now.  Ive been asked multiple times if it is possible to see if the Android keyboard is open using UiAutomator.  Ive always replied No, since it is not visible to the uiautomatorviewer, it was safe to assume that it could not be done.

They say pain is the only true catalyst for change and it was this pain that drove me to finally find the answer.  I found that the following adb command will tell you whether or not the keyboard is open:
adb shell dumpsys input_method | grep mInputShown
After running this command you may find that the information displayed is more than what you need.  So how do i find what Im looking for and use this in a Java function that I can call at any time during a Uiautomator test?  Here is the solution:


You can use this function to find out if the keyboard is currently open and then close it. This is the best way to make sure you don't attempt to close the keyboard when it isn't open which would result in clicking the Phones BACK button and some potential confusion as to what went wrong


If you disagree, have any questions or would be interested in seeing a tutorial about anything else Android or Automation related, hit me up on Google+.