8

我正在尝试测试AlertDialogwith ActivityInstrumentationTestCase2

这是原始代码:

    this.setmBtAppelerFixe(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(InterventionImmobiliereDetailsActivity.this);
            dialog.setTitle("Appel");
            dialog.setMessage("Appeler le contact ?");
            dialog.setCancelable(true);
            dialog.setNegativeButton("Non", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    InterventionImmobiliereDetailsActivity.this.lancerIntentAppel(mIntervention.getTelContact());
                }
            });

            mAdAppelerFixe = dialog.create();
            mAdAppelerFixe.show();
        }
    });

现在我无法点击正面按钮。这段代码似乎不起作用:

    mActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            assertTrue(mLLAppelerFixe.performClick());

            AlertDialog mDialog = mActivity.getAdAppelerFixe();
            assertTrue(mDialog.isShowing());

            Button okButton = mDialog.getButton(AlertDialog.BUTTON_POSITIVE);

            assertTrue(okButton.performClick());
            assertTrue(mActivity.isNumeroValide());
        }
    });

首先,我单击我的布局以打开AlertDialog. 然后我得到OK_BUTTON并单击它。它应该将numeroValide布尔值设置为 true。但什么都没有。

我怎样才能简单地测试一个AlertDialog按钮?

4

4 回答 4

4

这在我的 nexus 4 设备中运行良好:

@MediumTest
public void testStartMyActivity() {
    monitor = getInstrumentation().addMonitor(MyActivity.class.getName(), null, false);

    TouchUtils.clickView(this, startMyActivityButton);

    MyActivity myActivity = (MyActivity) monitor.waitForActivityWithTimeout(2000);
    assertNotNull("MyActivity activity not started, activity is null", myActivity);

    AlertDialog dialog = myActivity.getLastDialog(); // I create getLastDialog method in MyActivity class. Its return last created AlertDialog
    if (dialog.isShowing()) {
        try {
            performClick(dialog.getButton(DialogInterface.BUTTON_POSITIVE));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    myActivity.finish();
    getInstrumentation().removeMonitor(monitor);
}

private void performClick(final Button button) throws Throwable {
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            button.performClick();
        }
    });
    getInstrumentation().waitForIdleSync();
}

这里示例测试 AlertDialog(来自 android google 源): AlertDialogTest.java

于 2014-03-20T05:16:44.457 回答
4

在您的原始活动中,您可以创建一个简单的方法来返回最后一个 AlertDialog 的实例。

public AlertDialog getDialog(){
    return alertDialog;
}

在测试活动中,您可以使用以下代码访问警报对话框并单击按钮。

ActivityMonitor monitor =  getInstrumentation().addMonitor(MyActivity.class.getName(), null, false);
MyActivity myActivity = (MyActivity) monitor.waitForActivity();
getInstrumentation().waitForIdleSync();    

// access the alert dialog using the getDialog() method created in the activity
AlertDialog dialog = myActivity.getDialog();

// access the button
Button okBtn = (Button) dialog.findViewById(R.id.button_ok);
TouchUtils.clickView(this, okBtn);
getInstrumentation().removeMonitor(monitor);
于 2014-10-18T07:16:23.050 回答
1

要单击 AlertDialog 按钮,请使用 Espresso

点击正面按钮

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

并单击否定按钮

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

欲了解更多信息,请访问Espresso 示例对话框测试

于 2019-01-18T11:02:01.353 回答
0

您现在可以通过代码使用 Espresso

onView(withText("South China Sea"))
  .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
  .perform(click());

Espresso 样品

于 2014-04-28T08:16:07.637 回答