(Robotium) Action bar up / home button

I use Robotium as a library for Junit test for my Android application. I wrote some tests that work well. But when I try to write a test for my own ActionBar , press the Up / Home button , it failed.

My test code is very simple:

Solo solo = new Solo(getInstrumentation(), getActivity()); ... solo.clickOnActionBarHomeButton(); // I expected it will click the Up/Home button of ActionBar 

I expected the above code to press the Up / Home button of its own ActionBar, but it failed, nothing happened on the action bar. Why??

PS I am using an Android 4.2.1 device.

+4
source share
3 answers

I am using this function:

 public void clickOnActionBarHomeButton(Solo solo) { View homeView = solo.getView(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.id.home : R.id.home); solo.clickOnView(homeView); } 

This may not be the perfect solution, but it works.

+5
source

In our Home application, the button ultimately calls NavUtils.navigateUpFromSameTask . Using

 solo.clickOnActionBarHomeButton(); 

press the Home button, this will not work. However, I noticed that solo.clickOnScreen reliably clicks the Home button. Therefore, we use this method instead of solo.clickOnActionBarHomeButton :

 protected void clickOnHome() { solo.clickOnScreen(50, 50); //usually hits Home button } 
+1
source

You can click the Up / Home button (this is the first ImageButton element in activity) using the following code:

 solo.clickOnImageButton(0); instrumentation.waitForIdleSync(); 
0
source

All Articles