Android 使用浓缩咖啡测试软键盘是否可见
我想测试活动调用onCreate()和onResume()时键盘的可见性Android 使用浓缩咖啡测试软键盘是否可见,android,android-espresso,Android,Android Espresso,我想测试活动调用onCreate()和onResume()时键盘的可见性 如何测试是否使用浓缩咖啡显示键盘?另一个技巧可能是检查显示键盘时您知道将要覆盖的视图的可见性。别忘了考虑动画 使用浓咖啡和hamcrest对NOT matcher进行仪器测试,如: //make sure keyboard is visible by clicking on an edit text component ViewInteraction v = onView(withId(R.id.editText)
如何测试是否使用浓缩咖啡显示键盘?另一个技巧可能是检查显示键盘时您知道将要覆盖的视图的可见性。别忘了考虑动画 使用浓咖啡和hamcrest对NOT matcher进行仪器测试,如:
//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()));
在上找到的我知道,这个问题已经足够老了,但是没有任何公认的答案。 在我们的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)
}
}
希望对别人有用这对我很有用
private boolean isSoftKeyboardShown() {
final InputMethodManager imm = (InputMethodManager) getActivityInstance()
.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.isAcceptingText();
}
@igork答案的Java版本。此方法对我有效
val isKeyboardOpened: Boolean
get() {
for (window in InstrumentationRegistry.getInstrumentation().uiAutomation.windows) {
if (window.type == AccessibilityWindowInfo.TYPE_INPUT_METHOD) {
return true
}
}
return false
}
浓缩咖啡不需要sleep()。相反,您应该使用IdlingResource。另外,
Espresso.closeSoftKeyboard()
。动画应该停止使用Espresso进行测试,以确保一切正常。这对我不起作用。当一个输入被聚焦时,它返回true,但键盘被隐藏。对我来说也不起作用。始终返回false,即使EditText
处于焦点中。什么是UiDevice
?那是从哪个图书馆来的?这是从UiAutomator图书馆来的。太好了,如果editText是焦点,但键盘是关闭的,这个功能可以工作,而且似乎没有问题。太棒了。这对我来说确实有效
val isKeyboardOpened: Boolean
get() {
for (window in InstrumentationRegistry.getInstrumentation().uiAutomation.windows) {
if (window.type == AccessibilityWindowInfo.TYPE_INPUT_METHOD) {
return true
}
}
return false
}