简体   繁体   中英

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?

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

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

found at Google groups

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:

//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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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