【问题标题】:how to test MultiChoiceItems android dialog box如何测试 MultiChoiceItems android 对话框
【发布时间】:2016-07-12 15:34:42
【问题描述】:

我用

构建了一个AlertDialog
  public class ConstantDialogFragment extends DialogFragment {

    private AlertDialog.Builder builder;

    private AlertDialog alertDialog;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final int[] constantProtocol = {0};
        builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

        builder.setTitle(getResources().getString(R.string.some_message))
                .setMultiChoiceItems(R.array.some_choice, null,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which,
                                                boolean isChecked) {
                                // some method
                            }
                        })
                .setPositiveButton(getResources().getString(R.string.ok_dialog), new DialogInterface.OnClickListener() {

                    class LoadConfigTask {
                        private ProgressDialog dialog;
                        private Activity activity;

                        public LoadConfigTask(Activity activity) {
                            this.activity = activity;
                            dialog = new ProgressDialog(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
                        }

                        public void execute() {
                            try {
                                // some method

                            } catch (Exception e) {
                                // some method

                            }
                        }
                    }

                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // some method
                    }
                })
                .setNegativeButton(getResources().getString(R.string.cancel_dialog), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // some thing 
                    }
                });
        builder.create();
        alertDialog = builder.show();
        return alertDialog;
    }

    public AlertDialog getAlertDialog(){
        return alertDialog;
    }

然后我尝试了这个espresso 实现:

onView(withText("my first choice")).
                perform(click());

我得到了

NoMatchingViewException: No views in hierarchy found matching: with text: is my string"

我该如何解决?

【问题讨论】:

  • 这和你的对话框有什么关系?
  • @greenapps 我不太明白你的问题。我构建了一个多项目对话框,我需要在单击其中一个复选框后测试某些行为
  • 这个 onView 驻留在哪里?你为什么不发布完整的代码?从监听器代码开始。
  • @greenapps 这来自一个从MainActivity 调用的片段。顺便说一句,我没有测试盒子的Ok 按钮
  • 请发布完整的可复制代码。

标签: java android unit-testing android-espresso


【解决方案1】:

Android源代码中,我找到了有用的方法。所以要控制AlertDialog的项目,我必须这样做

ListView listView = alertDialog.getListView();
View child = listView.getChildAt(0);
child.performClick();

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2011-01-28
    • 2012-11-01
    • 1970-01-01
    • 2012-05-01
    • 2010-10-26
    相关资源
    最近更新 更多