Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if soft keyboard is visible using espresso

I want to test keyboard visibility when an activity calls onCreate() and onResume().

How can i test whether or not the keyboard is shown using espresso?

like image 348
Vinay Nagaraj Avatar asked Nov 28 '15 11:11

Vinay Nagaraj


2 Answers

I know, that the question is old enough, but it doesn't have any accepted answer though. In our UI tests we use this method, which uses some shell commands:

/**
 * This method works like a charm
 *
 * SAMPLE CMD OUTPUT:
 * mShowRequested=true mShowExplicitlyRequested=true mShowForced=false mInputShown=true
 */
fun isKeyboardOpenedShellCheck(): Boolean {
    val checkKeyboardCmd = "dumpsys input_method | grep mInputShown"

    try {
        return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
            .executeShellCommand(checkKeyboardCmd).contains("mInputShown=true")
    } catch (e: IOException) {
        throw RuntimeException("Keyboard check failed", e)
    }
}

Hope, it'll be useful for someone

like image 58
grine4ka Avatar answered Nov 19 '22 12:11

grine4ka


fun isKeyboardShown(): Boolean {
    val inputMethodManager = InstrumentationRegistry.getInstrumentation().targetContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    return inputMethodManager.isAcceptingText
}

found at Google groups

like image 41
igork Avatar answered Nov 19 '22 12:11

igork