avaaaaalyn
avaaaaalyn

Reputation: 1

How to do an espresso test on an Array Adapter within an List View?

My application gets search results and puts this in a list view but I am trying to test what that result it. Here is a screenshot of what the search result page looks like. My code for the displaying of the items is below, as well as what I've tried to use to test the data.

final AlertDialog.Builder builder = new AlertDialog.Builder(SearchItemsActivity.this);
                        ListView listView = findViewById(R.id.listView);
                        listView.setAdapter(listViewAdapter);

onData(anything()).inAdapterView(withId(R.id.listView)).atPosition(1)
            .check(matches(withText("Harry Potter Books - "+"In Stock"+"\n"+"Email: [email protected]\n"+"Full hardcover series\n")));

The error code I'm getting is

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Harry Potter Books - In Stock\nEmail: [email protected]\nFull hardcover series\n"' doesn't match the selected view.                                                                                   
 Expected: with text: is "Harry Potter Books - In Stock\nEmail: [email protected]\nFull hardcover series\n"
 Got: "AppCompatTextView{id=16908308, res-name=text1, visibility=VISIBLE, width=1080, height=215, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.AbsListView$LayoutParams@ce20b7f, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Harry Potter Books - In Stock\nEmail: [email protected]\nFull hardcover series\n, input-type=0, ime-target=false, has-links=false}"

Upvotes: 0

Views: 357

Answers (1)

robigroza
robigroza

Reputation: 589

Use the Barista library for UI tests, it is the easiest solution and makes everything easier. https://github.com/AdevintaSpain/Barista

With Barista, you can do it like this:

assertDisplayedAtPosition(R.id.list, 0, "text");
assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, "text");
assertDisplayedAtPosition(R.id.list, 0, R.string.hello_world);
assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, R.string.hello_world);
assertCustomAssertionAtPosition(R.id.list, 0, customViewAssertion);

You can find more info on the Github page, it covers everything you need.

Upvotes: 1

Related Questions