如何在Espresso中与alertdialog进行交互



我有一个测试,其中有一个Alertdialog,上面有一个"input"字段和按钮"Cancel"(id为button2(和"Ok"(id为button1(。首先,我必须在字段中输入值"1234",然后单击"确定"按钮。但它对我不起作用,测试失败了。

onView(withId(R.id.input)).perform(typeText("1234"));
closeSoftKeyboard();
click(R.id.button1);
Thread.sleep(5000);

您应该使用isDialog()RootMatcher:

onView(allOf(
isDescendantOfA(withId(R.id.input)),
isAssignableFrom(EditText.class)))
.inRoot(isDialog())
.perform(typeText("1234"))
.perform(closeSoftKeyboard());
onView(withText("Ok"))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click());
Thread.sleep(5000);

最新更新