Clicking on the action bar menu items in Robotium

I am trying to run some automated tests in Robotium. I have the following code in my application that 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 am trying to click on a 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 do not work due to an error:

 View is null and therefore cannot be clicked. 

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

 solo.clickOnView(solo.getView(R.id.logoutmenu)); solo.clickOnMenuItem("Share My Artists"); 
+7
source share
3 answers

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

+18
source

I could make it work with all 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); } } 
0
source

Just do this:

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

5 is a position that simply changes it to the position of your menu item First - 1, second - 2, etc.

0
source

All Articles