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

21

我希望在一个Activity的onCreate()和onResume()方法被调用时测试键盘的可见性。

如何使用Espresso测试键盘是否显示?

7个回答

19

我知道这个问题已经很老了,但是它还没有任何被接受的答案。 在我们的 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)
    }
}

希望对某人有用


UiDevice是什么?它来自哪个库? - Jeff Padgett
1
这是来自UiAutomator库的内容。https://developer.android.com/reference/androidx/test/uiautomator/UiDevice - grine4ka
1
太好了,这个代码可以正常工作,如果editText获得焦点但键盘关闭也不会出现问题。 - Jeff Padgett

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

Google groups上发现了相关的IT技术内容。


6
这对我没有起作用。当输入框获得焦点但键盘隐藏时,它返回了true。 - Lifes
1
对我也不起作用。即使EditText处于焦点状态,它始终返回false。 - Marvin Effing
对我没用...始终返回true(编辑文本具有焦点) - Venkataramanan

0

另一个技巧可能是检查一个视图的可见性,当键盘显示时,你知道它将被覆盖。不要忘记考虑动画...

使用Espresso和Hamcrest进行仪器测试,对于NOT匹配器,可以使用类似以下内容的语句:

//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()));

1
使用Espresso时,不应该需要sleep()。相反,您应该使用IdlingResource。另外,Espresso.closeSoftKeyboard() - Code-Apprentice
在使用Espresso进行测试时,应该关闭动画以确保一切正常运行。 - KraffMann

0

这对我很有效。

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

    return imm.isAcceptingText();
}

@igork答案的Java版本。


0
你可以使用标准的插入API来确认软键盘是否可见。
view.rootWindowInsets.isVisible(WindowInsets.Type.ime())

如果你正在使用compose-ui-test,你可以使用它的waitUntil { } API:
@get:Rule val rule = createAndroidComposeRule<TestActivity>()

rule.waitUntil {
  rule.activity.window.decorView.rootWindowInsets.isVisible(WindowInsets.Type.ime())
}

0

这个方法对我有效

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

0
fun checkIsKeyboardDisplayed(expectedIsDisplayed: Boolean) {
  val actualIsDisplayed: Boolean
  val checkKeyboardCmd = "dumpsys input_method | grep mInputShown"
  try {
    actualIsDisplayed = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
      .executeShellCommand(checkKeyboardCmd).contains("mInputShown=true")
  } catch (e: IOException) {
    throw RuntimeException("Keyboard check failed", e)
  }
  Assert.assertTrue(actualIsDisplayed == expectedIsDisplayed)
}`enter code here`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接