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

Jim*_*nts 13 android android-studio android-espresso android-uiautomator

我想使用Espresso按下面按钮,但我不确定如何.我应该获得资源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();
}
}
Run Code Online (Sandbox Code Playgroud)

android.support.test.espresso.NoMatchingViewException:找不到层次结构中的视图匹配:with text:is"GA NAAR INSTELLINGEN"

pio*_*543 34

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

您应该更改以下代码:

onView(withText("GA NAAR INSTELLINGEN")).perform(click());
Run Code Online (Sandbox Code Playgroud)

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

如果它不起作用,不要费心长时间使用Espresso另一个伟大的谷歌仪器测试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();
}
Run Code Online (Sandbox Code Playgroud)

希望它会有所帮助


gir*_*iii 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());


IHe*_*oid 6

到目前为止,由于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())
}
Run Code Online (Sandbox Code Playgroud)


Cor*_*han 5

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

        onView(withId(R.id.username))
                .perform(typeText("username"))
                .perform(closeSoftKeyboard())
        onView(withId(android.R.id.button1)).perform((click()))
Run Code Online (Sandbox Code Playgroud)

正如this answer更详细。