【问题标题】:How to use espresso to press a AlertDialog button如何使用 espresso 按下 AlertDialog 按钮
【发布时间】:2017-01-15 13:19:33
【问题描述】:

我想使用 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();
}
}

android.support.test.espresso.NoMatchingViewException:层次结构中找不到匹配的视图:与文本:是“GA NAAR INSTELLINGEN”

【问题讨论】:

  • 我认为你可以使用 withText() 方法而不是通过 id 匹配
  • onView(withText("GA NAAR INSTELLINGEN")).perform(click());不工作

标签: android android-studio android-uiautomator android-espresso


【解决方案1】:

AlertController 类中,Android 操作系统将 ID 设置为 AlertDialog 按钮。检查setupButtons 方法。例如,在撰写本文时,正按钮 id 是 16908313。所以你可以这样写

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

DIALOG_POSITIVE_BUTTON_ID = 16908313

否定按钮也适用于我

【讨论】:

  • 对我来说这是最好的解决方案。
【解决方案2】:

对于AlertDialog,分配给每个按钮的 id 是:

  • 阳性:android.R.id.button1
  • 否定:android.R.id.button2
  • 中性:android.R.id.button3

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

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      由于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())
      }
      

      【讨论】:

      • 谢谢! isDialog() 对我不起作用,但我能够找到 button1 和 button2。感谢您明确写出不同按钮如何与其 ID 号对齐。
      【解决方案5】:

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

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

      this answer 更详尽地详细说明。

      【讨论】:

      • 不知道是不是新东西,但是androidx.test.espresso.action.ViewActions有一个方法`closeSoftKeyboard()'
      【解决方案6】:

      根据StackOverflow类似问题:Check if a dialog is displayed with 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();
      }
      

      希望对你有帮助

      【讨论】:

      • 这对我有用,没有 check() 方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多