使用浓缩咖啡测试软键盘是否可见

Vin*_*raj 14 android android-espresso

我想在活动调用onCreate()和onResume()时测试键盘可见性.

如何测试键盘是否显示使用浓缩咖啡?

gri*_*4ka 12

我知道,这个问题已经够老了,但它没有任何公认的答案。在我们的 UI 测试中,我们使用这种方法,它使用一些 shell 命令:

/**
 * 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

希望对某人有用


小智 7

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

Google 群组中找到

  • 这对我不起作用。当输入获得焦点但键盘被隐藏时,它返回 true。 (5认同)