Test if soft keyboard is visible using espresso

前端 未结 4 1513
野的像风
野的像风 2021-02-19 02:08

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?

4条回答
  •  别跟我提以往
    2021-02-19 02:22

    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

提交回复
热议问题