在 AndroidTestCase 中访问 AlertDialog

发布于 2024-08-22 00:15:54 字数 201 浏览 10 评论 0原文

我正在使用 ActivityInstrumentationTestCase2 在我的 GUI 上进行自动黑盒测试。有没有办法单击对话框,或在单元测试中获取属于该对话框的视图?

我能想到的唯一方法是保留对对话框的引用,并让我的 Activity 实现一个 getter 方法来让测试用例访问该对话框。有没有更好的方法不需要更改我的生产代码?

I'm using ActivityInstrumentationTestCase2 to do automated black-box testing on my GUI. Is there a way to click on a dialog, or get Views belonging to the Dialog in unit tests?

The only way I could come up with is to keep a reference to the dialog and have my Activity implement a getter method to let testcases access the dialog. Is there a better way that doesn't require changing my production code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-08-29 00:15:54

是的,有一种更好的方法可以将 AlertDialogs 暴露给自动化代码,但您必须在生产代码中执行此操作。但这是值得的,因为它会让你的生活变得更轻松。让我解释一下。

您可以将 AlertDialogs 分配给 WeakHashMap 对象并非常轻松地检索它们。方法如下 -

//Definition for WeakHashMap Object
WeakHashMap< Integer, Dialog > managedDialogs = new WeakHashMap< Integer, Dialog  >();

//Some alertdialog builder that needs to be exposed
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivity.this);
switch(id)
    {
case DIALOG:
    alertDialogBuilder.setTitle("some title")
    .setMessage("some message")
    .setPositiveButton("button text", Onclick activity)         
    .setNeutralButton("button text", Onclick activity)          
    .setNegativeButton("button text", Onclick activity)         
.setCancelable(true);

    AlertDialog dialog = alertDialogBuilder.create();

    //Assigning the value of this dialog to the Managed WeakHashMap
    managedDialogs.put(DIALOG, dialog);
    return dialog;
    }

现在在您的测试框架中,当您期望出现对话框时,只需执行 -

AlertDialog dialog = (AlertDialog) activity.managedDialogs.get(YourActivity.DIALOG);

Yes, there is a better way of exposing the AlertDialogs to your automation code but you WILL have to do that in the production code. It will be worth it though cause it will make your life a lot easier. Let me explain.

You can assign your AlertDialogs to a WeakHashMap object and retrieve them very easily. Here is how -

//Definition for WeakHashMap Object
WeakHashMap< Integer, Dialog > managedDialogs = new WeakHashMap< Integer, Dialog  >();

//Some alertdialog builder that needs to be exposed
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivity.this);
switch(id)
    {
case DIALOG:
    alertDialogBuilder.setTitle("some title")
    .setMessage("some message")
    .setPositiveButton("button text", Onclick activity)         
    .setNeutralButton("button text", Onclick activity)          
    .setNegativeButton("button text", Onclick activity)         
.setCancelable(true);

    AlertDialog dialog = alertDialogBuilder.create();

    //Assigning the value of this dialog to the Managed WeakHashMap
    managedDialogs.put(DIALOG, dialog);
    return dialog;
    }

Now in your test framework, when you expect the dialog to appear, simply do -

AlertDialog dialog = (AlertDialog) activity.managedDialogs.get(YourActivity.DIALOG);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文