Clicking on Action Bar Menu Items in Robotium
Asked Answered
C

3

7

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"); 
Cheeseparing answered 13/4, 2013 at 21:51 Comment(1)
are you running your tests this on Android 2.3?Ellis
E
18

if you are running Robotium tests on Android 4.0+, consider using solo.clickOnActionBarItem().

Ellis answered 19/4, 2013 at 9:15 Comment(2)
Just for people wondering about the correct parameter, it is "int id" so don't put {0, 1, ...} but R.id.your_id. ; )Foolery
I want to click on overflow menu on the action bar and click on setings item. solo.clickOnActionBarItem(0x1); worked before but now its not working, I think something has changed i dont know what, appcompat action bar. I am testing using apk I dnt have access o R.id.xBiceps
R
1

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.

Roundlet answered 27/3, 2017 at 11:14 Comment(0)
S
0

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);
    }
}
Stickinthemud answered 28/4, 2015 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.