【问题标题】:How to interact with alertdialog in Espresso?如何与 Espresso 中的 alertdialog 交互?
【发布时间】:2020-03-02 13:34:25
【问题描述】:

我有一个测试,其中有一个 Alertdialog,其中有一个“输入”字段和按钮“取消”(id 是 button2)和“Ok”(id 是 button1)。首先,我必须在字段中输入值“1234”,然后单击“确定”按钮。但它对我不起作用,测试失败。

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

【问题讨论】:

    标签: testing android-espresso barista


    【解决方案1】:

    你应该使用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);
    

    【讨论】:

    • 尝试在确定按钮上使用withText。查看更新的答案
    • 过去,给出这样的错误“Cannot resolve method 'click()'”
    • 会不会是closeSoftKeyboard(); 隐藏了对话框?尝试不使用它或将其作为输入的操作移动
    • 在视图 'with id: by.belinvestbank.test:id/input' 上执行 'type text(1234)' 时出错。
    • 你在使用TextInputLayout吗?尝试检查 EditText 类型的后代。查看更新的答案
    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2011-01-25
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多