如何使用咖啡按 AlertDialog button

发布时间: 2016/10/10 20:05:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要压在下面 button 使用咖啡,但我不知道如何。我应该得到的资源 id?或如何将 ID 设置为 AlertDialog ??

enter image description here

@RunWith(AndroidJUnit4.class)
public class ApplicationTest {

@Rule
public ActivityTestRule<LoadingActivity> mActivityRule =
        new ActivityTestRule<>(LoadingActivity.class);

@Test
public void loginClickMarker() {
//Doesn't work:
    onView(withText("GA NAAR INSTELLINGEN")).perform(click());
}
}

public class PopupDialog {

public static void showGPSIsDisabled(Context context, String msg, final PopupDialogCallback popupDialogCallback) {
    new AlertDialog.Builder(context)
            .setTitle(context.getString(R.string.location_turned_off))
            .setMessage(msg)
            .setPositiveButton(context.getString(R.string.go_to_settings), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    popupDialogCallback.hasClicked();
                }
            }).show();
}
}

android.support.test.espresso.NoMatchingViewException︰ 没有视图层次结构中的找到匹配︰ 与文本︰ 是"GA NAAR INSTELLINGEN"

解决方法 1:

根据 StackOverflow 的类似问题︰检查如果 dialog 显示与咖啡

你应该改变你的代码︰

onView(withText("GA NAAR INSTELLINGEN")).perform(click());

onView(withText("GA NAAR INSTELLINGEN")))
    .inRoot(isDialog()) // <---
    .check(matches(isDisplayed()))
    .perform(click());

如果它不会工作,懒得用长 Espresso 另一个伟大谷歌的 instrumentation 测试,称为 uiatomator

检查︰ http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

示例代码︰

// Initialize UiDevice instance
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

// Search for correct button in the dialog.
UiObject button = uiDevice.findObject(new UiSelector().text("GA NAAR INSTELLINGEN"));
if (button.exists() && button.isEnabled()) {
    button.click();
}

希望它能帮助

赞助商