简体   繁体   中英

Clicking on Action Bar Menu Items in Robotium

I'm trying to run some automated tests in Robotium. I have the following code in my application which sets up an options menu :

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

I try to click on the menu in Robotium using the code :

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

However my tests fail due with the error :

View is null and therefore cannot be clicked.

I have also tried using the code below which also failed :

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

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

Just do that :

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

5 is the position just change it to the position of your Menu item the First is 1 , the Second is 2 etc.

I could get it working on all the SDKs, using this:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM