我是使用 Robotium 的新手,似乎在任何地方的论坛上都找不到任何答案。我有一个要测试的 PreferenceActivity。问题是每当我需要单击首选项时,我的单元测试都会失败。
更具体地说,我有一个 CheckBoxPreference 我正在运行一些测试。我想验证当 CheckBoxPreference 被选中时,屏幕上有某些首选项已启用(未变灰),反之亦然。截至目前,我什至无法使用searchText()
/waitForText()
方法找到首选项(这是屏幕上的第一个首选项)。在clickOnView()
从getView()
.
看来我现在能做的最好的事情就是使用 Android 的setChecked()
方法来操纵 CheckBoxPreference 的状态。我假设以这种方式进行测试是可能的,但是我正在研究一些基本的东西。由于 PreferenceActivity 也是 ListActivity,因此我也尝试搜索有关测试 ListActivity 的相关问题,但无济于事。
以下几个链接为我使用 PreferenceActivities 和 Robotium 提供了一个良好的开端。
这是我正在使用的代码:
public void testEnabledChecked() throws Exception {
CheckBoxPreference enabled = PrefTestingUtils.getCheckBoxPreference(mSolo, mActivity,
(String) getVal("KEY_ENABLED"));
EditTextPreference recipient = PrefTestingUtils.getEditTextPreference(mSolo, mActivity,
(String) getVal("KEY_RECIPIENT"));
EditTextPreference title = PrefTestingUtils.getEditTextPreference(mSolo, mActivity,
(String) getVal("KEY_TITLE"));
ListPreference interval = PrefTestingUtils.getListPreference(mSolo, mActivity,
(String) getVal("KEY_INTERVAL"));
// ensures that the preference is unchecked
if (!enabled.isChecked()) {
Log.d(TAG, "Enabled preference unchecked, clicking on it");
// mSolo.clickOnView(enabled.getView(null, null)); // Attempt #1
// mSolo.clickOnText("Enabled"); // Attempt #2
// mSolo.clickOnCheckBox(0); // Attempt #3
// mSolo.searchText("Enabled"); // cannot find view
}
assertTrue(enabled.isChecked()); // AssertionFailedError here
assertTrue(recipient.isEnabled());
assertTrue(title.isEnabled());
assertTrue(interval.isEnabled());
}
我已经验证 PrefTestingUtils 函数确实返回了对首选项的有效引用。所有 PrefTestingUtils 函数都与此类似:
public static CheckBoxPreference getCheckBoxPreference(Solo solo,
PreferenceActivity activity, String key) {
if (solo == null || activity == null || key == null) {
Log.d(TAG, "getCheckBoxPreference::Null parameter");
return null;
}
Preference p = activity.findPreference(key);
if (p instanceof CheckBoxPreference) {
return (CheckBoxPreference) p;
}
return null;
}
任何人都可以提供的任何帮助将不胜感激。谢谢!