Clicking on a specific child of a ListView in Espresso

Using Espresso, I would like to be able to click on a specific child of the ExpandableListView (named CustomExpandableView). Listview creates a set of RelativeLayouts (named MyContainer).

Ideally, I would like to click on a specific MyContainer in a CustomExpandableView. But I can live by simply clicking on the first.

MyContainer objects do not have unique identifiers that I can reference, but their children, for example. - "text = Example text here 1"

I tried several different use cases for onData, passing in the class type and trying to get the child in a specific position, but it just doesn't work. And I wanted to avoid getting the object and repeating it until I found the right child.

Here is part of the view hierarchy for the link (I removed important information from the hierarchy):

  + -----> CustomExpandableView {} 
 |
 + ------> LinearLayout {}
 |
 + -------> TextView {}
 |
 + -------> FrameLayout {}
 |
 + --------> BreadCrumbView {}
 |
 + ---------> ImageButton {}
 |
 + ---------> TextView {}
 |
 + ------> LinearLayout {}
 |
 + -------> MyContainer {}
 |
 + --------> ImageView {res-name = thumb,}
 |
 + --------> ImageView {res-name = divider}
 |
 + --------> TextView {res-name = label, text = Sample Text Here 1, input-type = 0, ime-target = false}
 |
 + -------> MyContainer {}
 |
 + --------> ImageView {res-name = thumb}
 |
 + --------> ImageView {res-name = divider}
 |
 + --------> TextView {res-name = label text = Sample Text Here 2, input-type = 0, ime-target = false}
 |
+6
source share
4 answers

Try using instanceOf in conjunction with a hasSibling match, for example.

 onView(allOf(is(instanceOf(MyContainer.class)), hasSibling(withText("Sample Text Here 1")))) 
0
source

You can follow the EspressoSamples directly here.
You should use onData() for example.

 onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("STR"), is("item: 50"))) .perform(click()); 
0
source

When you try to use onData , Espresso does not try to find a view by some parameters, but get the Adapter from the ListView and look for data in this adapter (by calling Adapter#getItem ).

But in the case of the ExpandableListView it is not clear what data needs to be obtained. And for that, Espresso provided onData...().usingAdapterViewProtocol(AdapterViewProtocol) . This javadoc method says:

/** * Use a different AdapterViewProtocol if the Adapter implementation does not * satisfy the AdapterView contract like (@code ExpandableListView) */

This AdapterViewProtocol interface looks like this: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/com/google/android/apps/common/testing /ui/espresso/action/AdapterViewProtocol.java .

AdapterViewProtocol implementation AdapterViewProtocol : fooobar.com/questions/957085 / ...

Once correctly identified, onData will find the search for the item you were looking for. After that, if you need to perform an action or check it. You just have to onData#onChildView .

0
source
 val listViewItem = onData(anything()).inAdapterView(withId(android.R.id.list)).atPosition(10) listViewItem.onChildView(withId(android.R.id.checkbox)).check(matches(isChecked())) 

This will be useful if you know the presentation position where you need to access the child. Here, in the first line, I got the 10th position view (ieviewgroup) from the list view. In the second line, I get access to its child "flag" and approve it. For your information: this is in kotlin, and you can convert the same in java using android studio.

0
source

Source: https://habr.com/ru/post/957084/


All Articles