[英]How to use robotium to check if an action bar menu item is present
I have some menu items in an ActionBar that are modified dynamically (for example, if the user logs in, I remove the 'login' menu item and replace it with a 'logout' item, etc). 我在ActionBar中有一些菜单项是动态修改的(例如,如果用户登录,则删除“登录”菜单项,并用“注销”项替换,等等)。
So basically I'm trying to test this functionality using robotium and I figured I'd try to load a view and check if it is present or not, something like: 因此,基本上,我试图使用robotium来测试此功能,并且我想尝试加载视图并检查其是否存在,例如:
assertNull(solo.getView(R.id.action_login)); // action_login is an action bar menu item
But this approach isn't working since I'm getting the following error: 但是,由于出现以下错误,该方法无法正常工作:
View with id: 'XXX' is not found!
Any idea how to test this kind of functionality? 知道如何测试这种功能吗? Thanks 谢谢
The easiest way would be to make a helper method along the lines of: 最简单的方法是按照以下方式创建一个辅助方法:
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));
My approach that I've whipped up minutes ago is as following: 我在几分钟前想通的方法如下:
Click on the overflow button itself (the three dots). 单击溢出按钮本身(三个点)。 To do that, find a view of type ActionMenuView. 为此,找到一个类型为ActionMenuView的视图。 This can be done as following: 可以按照以下步骤完成:
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));
Waiting for the view with id equal to proper item id failed me. 等待ID等于正确项目ID的视图使我失败了。 So what I did instead is I waited for item's string to appear. 所以我要做的是等待项目的字符串出现。
assertTrue("Action item not present", solo.waitForText( solo.getCurrentActivity().getString(R.string.my_action_string), 1, 500 ) );
As one may notice, this method has serious drawbacks - not only the uniqueness of action bar item string is required, but also clicking on the overflow might fail if there are other action bar buttons visible. 可能已经注意到,此方法具有严重的缺陷-不仅要求操作栏项目字符串具有唯一性,而且如果看到其他操作栏按钮,则单击溢出可能会失败。 Other than that, it works in my case - test implemented as above passes. 除此之外,它在我的情况下仍然有效-通过上述测试即可通过。 However, if the line about clicking the overflow button is commented out, it fails (as expected). 但是,如果有关单击溢出按钮的行被注释掉,则它将失败(按预期)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.