gpt4 book ai didi

安卓测试: how to check a dialog is displayed on screen?(使用ActivityInstrumentationTestCase2)

转载 作者:可可西里 更新时间:2023-11-01 18:45:40 25 4
gpt4 key购买 nike

我试图最终将 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() 显示它

最佳答案

因为您已经在使用 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"));
}

关于安卓测试: how to check a dialog is displayed on screen?(使用ActivityInstrumentationTestCase2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com