7

我正在尝试在 Robotium 中运行一些自动化测试。我的应用程序中有以下代码,它设置了一个选项菜单:

  public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.layout.logoutmenu, menu);
        return super.onCreateOptionsMenu(menu);
   }

我尝试使用以下代码单击 Robotium 中的菜单:

solo.sendKey(Solo.MENU);
solo.clickOnView(solo.getView(R.id.share)); //share is the id of the menu item

但是我的测试因错误而失败:

View is null and therefore cannot be clicked.

我也尝试过使用下面的代码,但也失败了:

solo.clickOnView(solo.getView(R.id.logoutmenu));
solo.clickOnMenuItem("Share My Artists"); 
4

3 回答 3

18

如果您在 Android 4.0+ 上运行 Robotium 测试,请考虑使用solo.clickOnActionBarItem().

于 2013-04-19T09:15:04.330 回答
1

这样做:

solo.sendKey(Solo.MENU);
solo.clickInList(5);

5 是位置,只需将其更改为您的菜单项的位置 First 是 1 , Second 是 2 等等。

于 2017-03-27T11:14:34.530 回答
0

我可以让它在所有的 SDK 上工作,使用这个:

View ab = _solo.getCurrentActivity().findViewById(R.id.action_bar);
ArrayList<View> views = _solo.getViews(ab);
for (int i = 0; i <views.size(); i++) {
    if (views.get(i).getClass().getName().contains("ActionMenuPresenter")) {
        _solo.clickOnView(views.get(i));
        _solo.waitForText(SOME_TEXT);
    }
}
于 2015-04-28T16:11:17.050 回答