如何使用浓缩咖啡按下 AlertDialog 按钮

问题描述 投票:0回答:6

我想使用浓缩咖啡按下面的按钮,但我不知道如何操作。我应该获取资源 ID 吗?或者如何给AlertDialog设置一个ID??

@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”

android android-studio android-uiautomator android-espresso
6个回答
58
投票

根据

StackOverflow
类似问题:检查Espresso是否显示对话框

您应该将代码更改为:

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

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

如果它不起作用,请不要费心长时间使用

Espresso
另一个很棒的 Google 仪器测试,称为
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();
}

11
投票

由于

inRoot(isDialog())
似乎不适用于
DialogFragment
我到目前为止使用此解决方法:

enum class AlertDialogButton(@IdRes val resId: Int) {
    POSITIVE(android.R.id.button1),
    NEGATIVE(android.R.id.button2),
    NEUTRAL(android.R.id.button3)
}

fun clickOnButtonInAlertDialog(button: AlertDialogButton) {
    onView(withId(button.resId)).perform(click())
}

7
投票

对于

AlertDialog
,为每个按钮分配的 id 为:

  • 积极:
    android.R.id.button1
  • 负面:
    android.R.id.button2
  • 中性:
    android.R.id.button3

你可以在

AlertController
类,
setupButtons()
方法中检查自己。 所以你可以执行如下点击:

onView(withId(android.R.id.button1)).perform(click());


6
投票

您可能需要像这样关闭软键盘:

        onView(withId(R.id.username))
                .perform(typeText("username"))
                .perform(closeSoftKeyboard())
        onView(withId(android.R.id.button1)).perform((click()))

此答案更详细地介绍。


3
投票

上次更新的好处是它允许您在每次测试之前设置权限,这样您就不必单击对话框(这大大加快了测试速度!)

使用以下规则(例如位置/存储......用您显然需要的权限替换这些规则)

@Rule
  public GrantPermissionRule runtimePermissionRule = GrantPermissionRule.grant(
      ACCESS_FINE_LOCATION, READ_EXTERNAL_STORAGE);

3
投票

AlertController
类中,Android 操作系统将 id 设置为 AlertDialog 按钮。检查
setupButtons
方法。例如,在撰写本文时,肯定按钮 ID 为 16908313。因此您可以编写如下内容

onView(withId(DIALOG_POSITIVE_BUTTON_ID)).check(matches(isDisplayed()));

其中

DIALOG_POSITIVE_BUTTON_ID
= 16908313

它也适用于我的否定按钮

© www.soinside.com 2019 - 2024. All rights reserved.