Issue
I'm using futuresimple's Android Floating Action Button. I'd like to use Espresso to create UI tests that interact with it. multiple_actions is just the button that opens the Floating Action Button (FAB) menu. draw_fab is one of the floating action buttons that appears when you click multiple_actions. Clicking draw_fab starts a new android activity and in this activity I wish to press a standard button with the id componentMenuButton (that itself brings up a menu).
@Test
public void simpleCircuitTest() {
onView(withId(R.id.multiple_actions)).perform(click());
onView(withId(R.id.draw_fab)).perform(click());
onView(withId(R.id.componentMenuButton)).perform(click());
// other stuff...
}
When I run this test I see that the first click works and the floating menu buttons display. However the call to click draw_fab seems to have zero affect and as such I get a No views in hierarchy found matching: with id... error when the call to click componentMenuButton is reached.
This is where I get confused.
@Test
public void simpleCircuitTest() {
onView(withId(R.id.draw_fab)).check(matches(isDisplayed()));
View v = mActivityRule.getActivity().findViewById(R.id.draw_fab);
Log.d(TAG, String.valueOf(v.getVisibility()==v.VISIBLE));
Log.d(TAG, String.valueOf(v.isShown()));
Log.d(TAG, String.valueOf(v.isEnabled()));
onView(withId(R.id.multiple_actions)).perform(click());
onView(withId(R.id.draw_fab)).check(matches(isDisplayed()));
Log.d(TAG, String.valueOf(v.getVisibility()==v.VISIBLE));
Log.d(TAG, String.valueOf(v.isShown()));
Log.d(TAG, String.valueOf(v.isEnabled()));
}
Above is my attempt to figure out what is going on. When I run this test even though I have not yet clicked the multiple_actions FAB, isDisplayed passes and all the log output is true. And then in the next segment Espresso clicks multiple_actions and again all the output is true. So it is as if performing the click on multiple_actions to bring up draw_fab for clicking has zero impact whatsoever on the test passing. This is not how it should be?
My hunch now is that the repo I'm using for floating action buttons simply doesn't support Espresso use? That or there's something special about FABs or something basic about Espresso that I'm missing. Which is it?
Solution
If I look at my activity_home.xml for the activity I'm trying to test I have the following for the FABs (Floating Action Buttons)
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/multiple_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
fab:fab_addButtonColorNormal="@color/colorPrimary"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonPlusIconColor="@color/white"
fab:fab_labelStyle="@style/menu_labels_style"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/draw_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/colorPrimary"
fab:fab_title="Draw Circuit"
fab:fab_colorPressed="@color/white_pressed"
fab:fab_icon="@drawable/ic_draw"/>
So I naturally thought that the id multiple_actions can be used to click the FAB that opens the menu of FABs. However, looking at a values.xml I have in my project that was generated by the FAB library I use I see
<item name="fab_expand_menu_button" type="id"/>
This likely comes from here. Using this ID (which I didn't write) instead, everything is fixed. I assume the FAB id is hard-coded for the top level one.
@Test
public void simpleCircuitTest() {
onView(withId(R.id.fab_expand_menu_button)).perform(click());
onView(withId(R.id.draw_fab)).perform(click());
onView(withId(R.id.componentMenuButton)).perform(click());
// other stuff...
}
More interestingly, when I use multiple_actions I figured out it presses elsewhere on the screen (didn't realize since there was no visual feedback, but then I placed a HorizontalScrollView to take up the entire screen and sure enough I see Espresso clicking it), which makes sense of why those log values always returned true. The values are true for another element on screen that isn't tied to being visible or not from clicking the FAB
Answered By - Frikster
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.