【问题标题】:Android testing: how to check a dialog is displayed on screen? (using ActivityInstrumentationTestCase2)Android测试:如何检查屏幕上显示的对话框? (使用活动检测测试用例 2)
【发布时间】:2011-11-03 12:37:53
【问题描述】:

我正在尝试最终将 UI 测试添加到我的 Android 应用程序中,以增加覆盖率(我的所有其他层都经过了正确测试,因此我所有的错误现在都来自 UI...) 我开始使用ActivityInstrumentationTestCase2 作为我的模拟器单元测试的基类,简单的事情很容易检查并且工作得很好。

但现在,我正在尝试检查对话框是否按预期显示,但我不知道该怎么做。

我的测试:

public void testOpensAboutDialogWhenAboutButtonClicked() {
    final MyActivity activity = getActivity();
    final Instrumentation instrumentation = getInstrumentation();

    final Button aboutButton = (Button) activity.findViewById(R.id.about);
    TouchUtils.clickView(this, aboutButton);

    // how to test for the AboutDialog?
}

现在我的对话框没有 id,所以我无法使用 findViewById 获得指向它的指针。 它是使用可用的构建器类创建的:

final AlertDialog about = new AlertDialog.Builder(parent)
            .setTitle(parent.getString(R.string.about_title))
            .setCancelable(true)
            .setIcon(R.drawable.skull)
            ....

有什么想法或教程的指针吗?

编辑:为了回答 Jens 的评论,我使用托管对话框,只是创建 AlertDialog 并使用 .show() 显示它

【问题讨论】:

  • 您是否使用托管对话框?
  • @Jens,其实我不是,我什至不知道他们......我只是读到他们,这可能确实有助于解决我的问题......(这会教我跳过教程...)今晚会尝试。如果是这样,请写一个答案,以便我可以给你赏金;)
  • 顺便说一句,我可以给你一个正确的答案。

标签: android unit-testing


【解决方案1】:

既然您已经在使用ActivityInstrumentationTestCase2,您应该开始使用Robotium - 它会大大简化您的测试

对于您的情况,就这么简单(如果您知道预期的标题或其他关于您的对话模糊独特的内容):

public void testSomeRandomSentence() {
    Solo solo = new Solo(getInstrumentation(), getActivity());
    getInstrumentation().waitForIdleSync();
    // Now do whatever you need to do to trigger your dialog.

    // Let's assume a properly lame dialog title.
    assertTrue("Could not find the dialog!", solo.searchText("My Dialog Title"));
}

【讨论】:

  • 有趣。我去看看。
  • 好吧,这看起来很有希望,绝对是适合我的方式。恭喜你,你赢得了赏金!
  • 所以在使用 Robotium 时这对我不起作用。我认为问题可能在于我使用的是旧式对话框(而不是DialogFragment),并且不知何故这与视图层次结构没有以相同的方式连接,因此solo.searchText(..) 找不到它。我通过使用 DialogFragment 和 Robotiums solo.waitForFragmentByTag(...) 方法解决了这个问题 - 现在效果很好:)
【解决方案2】:

在 setUp() 中将 id 分配给 Toast 之后

toast = (Toast)activity.findViewById(..........);

创建测试用例() {

ViewAsserts.assertOnScreen(toasts.getRootView(), toast.getRootView());
//pass if toast is visible on screen

}

【讨论】:

  • 感谢您的回答,但它不是 Toast,它是 AlertDialog,我的问题的重点是它没有 id,所以我不能使用 findViewById,我无法获得对对话框的引用。
【解决方案3】:

在您的对话框中添加一个吸气剂,例如:

public AlertDialog get_aboutbox()
{
   return this.about;
}

那么这里是你的测试解决方案:

public void testOpensAboutDialogWhenAboutButtonClicked() {
    final MyActivity activity = getActivity();

    assertNotNull("aboutbox is null",activity.get_aboutbox());
    final Instrumentation instrumentation = getInstrumentation();

    final Button aboutButton = (Button) activity.findViewById(R.id.about);
    TouchUtils.clickView(this, aboutButton);

    assertTrue("About Button didn't displayed the Dlg",
               activity.get_aboutbox().isShowing());
}

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 2022-08-19
    • 1970-01-01
    • 2017-03-02
    相关资源
    最近更新 更多