我正在尝试最终将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()
显示答案 0 :(得分:20)
由于您已经在使用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"));
}
答案 1 :(得分:1)
通过
在setUp()中为Toast分配id后toast = (Toast)activity.findViewById(..........);
创建testcase() {
ViewAsserts.assertOnScreen(toasts.getRootView(), toast.getRootView());
//pass if toast is visible on screen
}
答案 2 :(得分:0)
在对话框中添加一个getter,如:
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());
}