我是使用Robotium的新手,似乎無法在任何地方的論壇上找到任何答案。我有一個PreferenceActivity,我試圖測試。問題是,當我需要點擊一個首選項時,我的單元測試失敗。使用Robotium測試PreferenceActivity
更具體地說,我有一個CheckBoxPreference,我正在運行一些測試。我想驗證當CheckBoxPreference被選中時,屏幕上的某些首選項已啓用(不會變灰),反之亦然。截至目前,我甚至無法通過使用searchText()
/waitForText()
方法(它是屏幕上的第一首選項)找到首選項。在從getView()
獲得首選項的視圖後,我也嘗試使用clickOnView()
方法。
看來我現在能做的最好的事情就是使用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;
}
任何人都可以給任何幫助將不勝感激。謝謝!
我已經成功測試過PreferenceActivity。你有什麼嘗試?你應該發佈你當前的測試類代碼,否則很難給你指針。 – dmon
謝謝,dmon。我編輯了我的帖子以包含我的代碼。 – rach5000