Issue
This is how espresso dumps out the view hierarchy:
+------->AppBarLayout{id=-1, visibility=VISIBLE, width=1080, height=273, has-focus=false,...
|
+-------->Toolbar{id=2131296382, res-name=main_toolbar, visibility=VISIBLE, width=1080, height=147, ,...
|
+--------->AppCompatTextView{id=-1, visibility=VISIBLE, width=185, height=71, has-focus=false, ,...
|
+--------->AppCompatImageButton{id=-1, desc=Open navigation drawer, visibility=VISIBLE, width=147, height=147, ,...
|
+--------->ActionMenuView{id=-1, visibility=VISIBLE, width=359, height=147, has-focus=false, has-focusable=true, ,...
|
+---------->ActionMenuItemView{id=2131296286, res-name=action_toggle_location_service, desc=Toggle location service, ,...
The confusing thing is that visually the hamburger menu is on the first place, the App-Title is on the second place.
I struggled with the following test:
final ViewInteraction textView = onView(
allOf(
withParent(withId(R.id.main_toolbar)),
withParentIndex(1)
)
);
// Test fails!!! with withParentIndex(1)
textView.check(matches(withText("MobiAd")));
The above sample works with withParentIndex(0)
Does anyone know if the difference between UI-Automator and espresso is a bug or is there a reason for this?
Solution
I did a small sample app and I think I've figured out what's happening.
The app you're testing might have a custom toolbar which is set by setSupportActionBar().
As your application has a navigation drawer, the hamburger button will be displayed on your custom toolbar, apparently(as layout inspector shows) modifying its hierarchy.
For example:
<android.support.v7.widget.Toolbar
android:id="@+id/R.id.main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c13030">
<!--This textview exists at index 0-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MobiAd"/>
</android.support.v7.widget.Toolbar>
In your activity you will might find something like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
//here is the magic
setSupportActionBar(toolbar);
When navigation drawer button is displayed, it appears on the toolbar being a part of it, but STILL, your textview exists at index 0, not 1.
If you want to see the real hierarchy just take a look over what you have inside the toolbar(find the layout file where R.id.main_toolbar is declared).
SUGGESTION: Why do you want to find a text by looking to its index? If it will be moved to another index, you'll have to refactor your test.
You can use its string resource key(instead of writing several lines, you could do a simple check in a single line of code): onView(withText(R.string.toolbar_title)).check(matches(isDisplayed()));
Answered By - mbob


0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.