【发布时间】:2012-10-24 16:18:47
【问题描述】:
所有使用 Robotium 进行 GUI 测试的人。
你能告诉我什么 Android 原生测试框架不能做 Robotium 可以做吗? 据我所知,Robotium 可以用作黑盒测试,所以我不需要了解应用程序资源。 还有什么?
【问题讨论】:
标签: android robotium ui-testing
所有使用 Robotium 进行 GUI 测试的人。
你能告诉我什么 Android 原生测试框架不能做 Robotium 可以做吗? 据我所知,Robotium 可以用作黑盒测试,所以我不需要了解应用程序资源。 还有什么?
【问题讨论】:
标签: android robotium ui-testing
优点:
缺点:
android:process标签用于不同活动的多进程应用程序。示例代码:
优点:
缺点:
仅适用于 Android 4.1 或更高版本!
当您获得 ui-object 实例时,您不能使用源 ID。这意味着如果应用程序结构在一个布局上发生更改,您需要重构您的测试(这个问题可以通过为每个 ui 元素使用标签来解决)
您无法获取当前活动或仪器。这意味着您在开发测试方面受到限制,并且没有使用许多 android 的 api 方法进行测试。
很难调试,您需要有脚本来快速构建和启动测试并查看输出
层次结构查看器:
我认为这两个工具都很好,在测试期间在应用程序之间切换的可能性绝对很棒。您应该根据需要选择工具。我建议将 Robotium 用于不需要在应用程序之间切换的测试,因为它具有简单的方法和机会,可以使用 Android API 以简短的代码编写灵活和智能的测试,甚至可以覆盖 webview 和 milti-process 中的网页测试应用程序非常不寻常。
【讨论】:
Robotium 和原生工具之间的区别在于,使用 Robotium 编写测试非常简单。它基本上是来自 Solo 实例对象的指向和单击。
Here你可以下载JAR文件和一个示例项目自行测试。
更新
例如,我正在测试一个带有一些编辑文本、一个微调器和一个在我单击微调器选项时显示的弹出对话框的 Activity。请注意,使用其他方法,填充弹出对话框的字段非常痛苦。
这里是如何声明测试类和Robotium的初始化
import com.jayway.android.robotium.solo.Solo; //Using Robotium
//Robotium uses ActivityInstrumentationTestCase2.
//Note here the use of the template
public class AddNewQuestionTests extends
ActivityInstrumentationTestCase2<AddNewQuestion> {
public AddNewQuestionTests(Class<AddNewQuestion> name) {
super(name);
}
public AddNewQuestionTests() {
super(AddNewQuestion.class);
}
private Solo solo;
protected void setUp() throws Exception {
super.setUp();
//Initialize Solo with the instrumentation and the activity under test
solo = new Solo(getInstrumentation(), getActivity());
}
这里是测试方法:
public void testHappyPathAddScaleQuestion() {
// Type question title
solo.clickOnEditText(0); //find the EditText, and click it
solo.enterText((EditText) getActivity().findViewById(
//find the EditText, and put some string
R.id.activity_add_new_question_editText_question_title),
"Question title scale ");
// Type question description
solo.clickOnEditText(1);
solo.enterText((EditText) getActivity().findViewById(
R.id.activity_add_new_question_editText_question_description),
"Question description scale");
// Type the question
solo.clickOnEditText(2);
solo.enterText((EditText) getActivity().findViewById(
R.id.activity_add_new_question_editText_question),
"Question scale");
// Click the spinner and then the "Scale" question type
//Press an spinner option
solo.pressSpinnerItem(0, 4);
//Wait for the popup dialog title to show up. When robotium reads it, continue working solo.waitForText(getActivity().getResources().getString(R.string.activity_add_new_question_scale_selection_dialog_message));
// Type minimum and maximum ranges
solo.clickOnEditText(0);
solo.searchText(getActivity().getResources().getString(R.string.activity_add_new_question_maximum_value_hint));
solo.clickOnView(solo.getCurrentEditTexts().get(0));
solo.enterText(0, "34");
solo.clickOnView(solo.getCurrentEditTexts().get(0));
solo.enterText(1, "55");
// Click ok to close the dialog
solo.clickOnButton(getActivity().getResources().getString(R.string.OK));
// Click ok to get an ok message
solo.clickOnButton(getActivity().getResources().getString(R.string.OK));
//Wait for the ok toast message
boolean flagOKDatabase=solo.waitForText(getActivity().getResources().getString(R.string.database_success_storing_data),1,120);
assertEquals("Something wrong happened with the database", true, flagOKDatabase);
}
【讨论】:
在原生于 android 的检测框架上使用 robotsium 没有任何缺点。那是因为在使用 robotium 时,您仍然可以在没有它的情况下做任何事情,但您也可以使用许多有用的功能。
还有其他一些 android 自动化框架,但绝对值得一看。如果您在 Web 视图中有任何代码,这些代码会特别有用,因为这是 robotsium 真正让自己失望的地方。
https://github.com/calabash/calabash-android
【讨论】: