[英]How to use robotium to check if an action bar menu item is present
我在ActionBar中有一些菜單項是動態修改的(例如,如果用戶登錄,則刪除“登錄”菜單項,並用“注銷”項替換,等等)。
因此,基本上,我試圖使用robotium來測試此功能,並且我想嘗試加載視圖並檢查其是否存在,例如:
assertNull(solo.getView(R.id.action_login)); // action_login is an action bar menu item
但是,由於出現以下錯誤,該方法無法正常工作:
View with id: 'XXX' is not found!
知道如何測試這種功能嗎? 謝謝
最簡單的方法是按照以下方式創建一個輔助方法:
public boolean viewExist(int id){
try{
solo.getView(id);
return true;
}catch(AssertionError e){
return false;
}
}
您可以調用菜單項並驗證調用是否成功;
assertTrue(getInstrumentation().invokeMenuActionSync(activity, R.id.action_login, 0));
我在幾分鍾前想通的方法如下:
單擊溢出按鈕本身(三個點)。 為此,找到一個類型為ActionMenuView的視圖。 可以按照以下步驟完成:
assertTrue("Couldn't fetch overflow button", solo.waitForView(ActionMenuView.class, 1, 500)); ArrayList<ActionMenuView> actionMenuView = solo.getCurrentViews(ActionMenuView.class); assertFalse("Couldn't fetch overflow button", actionMenuView.isEmpty()); solo.clickOnView(actionMenuView.get(0));
等待ID等於正確項目ID的視圖使我失敗了。 所以我要做的是等待項目的字符串出現。
assertTrue("Action item not present", solo.waitForText( solo.getCurrentActivity().getString(R.string.my_action_string), 1, 500 ) );
可能已經注意到,此方法具有嚴重的缺陷-不僅要求操作欄項目字符串具有唯一性,而且如果看到其他操作欄按鈕,則單擊溢出可能會失敗。 除此之外,它在我的情況下仍然有效-通過上述測試即可通過。 但是,如果有關單擊溢出按鈕的行被注釋掉,則它將失敗(按預期)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.