How do I write junit test cases for actionbar items in android ? Any way of getting its reference for performing click events on it ?
You can simulate clicking an ActionBar item like this:
public void testButton(){
final View view = activity.findViewById(com.example.R.id.button1);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
view.requestFocus();
view.callOnClick();
}
});
}
In this following example, i'm able to retrieve the navigation tab button of the action bar (native or ActionBarSherlock). Then i click on them with TouchUtils.clickView():
try {
// Trying to get the ActionBar view '@id/android:action_bar_container' dynamically
int resId =
a.getResources().getIdentifier("action_bar_container", "id", "android");
View actionBarContainer = a.findViewById(resId);
// The class 'com.android.internal.widget.ActionBarContainer' must be in
// the classpath of this test project to be able to call
// the method 'getTabContainer' at runtime
Method getTabContainer =
com.android.internal.widget.ActionBarContainer.class.getMethod("getTabContainer",
(Class<?>[]) null);
HorizontalScrollView tabContainer =
(HorizontalScrollView) getTabContainer.invoke(actionBarContainer, (Object[]) null);
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);
} catch (Exception e) {
// Trying with SherlockActionBar
com.actionbarsherlock.internal.widget.ActionBarContainer actionBarContainer =
(com.actionbarsherlock...) a.findViewById(R.id.abs__action_bar_container);
HorizontalScrollView tabContainer =
(HorizontalScrollView) actionBarContainer.getTabContainer();
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);
}
}
use robotium.jar library
import com.jayway.android.robotium.solo.Solo;
private Solo solo;
this.solo = new Solo(getInstrumentation(),getActivity());
//R.id.menu_action_signup Menu Iten id.
this.solo.clickOnView(this.solo.getView(R.id.menu_action_signup));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With