简体   繁体   English

使用 espresso 测试软键盘是否可见

[英]Test if soft keyboard is visible using espresso

I want to test keyboard visibility when an activity calls onCreate() and onResume().我想在活动调用 onCreate() 和 onResume() 时测试键盘可见性。

How can i test whether or not the keyboard is shown using espresso?如何使用 espresso 测试键盘是否显示?

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:在我们的 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)
    }
}

Hope, it'll be useful for someone希望对某人有用

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

found at Google groupsGoogle 群组中找到

another trick could be checking for the visibility of a view that you know is going to be covered when the keyboard is showing.另一个技巧可能是检查您知道在键盘显示时将被覆盖的视图的可见性。 don't forget to take animations into consideration...不要忘记考虑动画......

instrumentation testing using espresso and hamcrest for the NOT matcher something like:使用 espresso 和 hamcrest 进行非匹配器的仪器测试,例如:

//make sure keyboard is visible by clicking on an edit text component
    ViewInteraction v = onView(withId(R.id.editText));
    ViewInteraction v2 = onView(withId(R.id.componentVisibleBeforeKeyboardIsShown));
    v2.check(matches(isDisplayed()));
    v.perform(click());
    //add a small delay because of the showing keyboard animation
    SystemClock.sleep(500);
    v2.check(matches(not(isDisplayed())));
    hideKeyboardMethod();
    //add a small delay because of the hiding keyboard animation
    SystemClock.sleep(500);
    v2.check(matches(isDisplayed()));

This works for me.这对我有用。

private boolean isSoftKeyboardShown() {
    final InputMethodManager imm = (InputMethodManager) getActivityInstance()
           .getSystemService(Context.INPUT_METHOD_SERVICE);

    return imm.isAcceptingText();
}

Java version of @igork's answer. @igork 答案的 Java 版本。

This method is working for me这种方法对我有用

val isKeyboardOpened: Boolean
    get() {
        for (window in InstrumentationRegistry.getInstrumentation().uiAutomation.windows) {
            if (window.type == AccessibilityWindowInfo.TYPE_INPUT_METHOD) {
                return true
            }
        }
        return false
    }

This is a kind of trick to check if keyboard is visible, it's not a perfect solution but for me was enough: 这是一种检查键盘是否可见的技巧,这不是一个完美的解决方案,但对我来说已经足够了:

  1. check if the fragment/activity container is displayed 检查是否显示了片段/活动容器
  2. perform a press back 执行新闻
  3. check if the same fragment/activity container is displayed 检查是否显示了相同的片段/活动容器

Simple code example: 简单的代码示例:

onView(allOf(withId(R.id.myFragment),isDisplayed()));
onView(withId(R.id.myFragment)).perform(pressBack());
onView(allOf(withId(R.id.myFragment),isDisplayed()));

If keyboard is visible means that the second time you press back button the view container is still there ;) 如果键盘可见,则表示您第二次按下返回按钮,视图容器仍然存在;)

Hope this help! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM