Android espresso - How to check value of TextView at the bottom of Listview?

我的未来我决定 提交于 2020-01-05 06:51:12

问题


In the attached image above, it is a ListView having TextView (delivery report)

Its status can be 'Sent' or 'Sending' or 'Failed'

I want to check for 'Sent' condition which means assert message sent successfully

As it is a conversation, newer messages will be at the bottom of the listview.

What I've tried is...

    // Type the message
    ViewInteraction smsEditText = onView(withId(R.id.text_editor)).check(matches(isDisplayed()));
    smsEditText.perform(typeText("abcde"));

    closeSoftKeyboard();

    Thread.sleep(500);

    // Click on send Button 
    ViewInteraction smsSendButton = onView(withId(R.id.composebtnSend)).check(matches(isDisplayed()));
    smsSendButton.check(matches(isEnabled()));
    smsSendButton.perform(click());

    Thread.sleep(3000);

    // Assert for msg Sent delivery report
    onData(anything()).inAdapterView(withId(R.id.history)).atPosition(2)
            .onChildView(withId(R.id.timestamp)).check(matches(withText("Sent .")));

Position should be 'listAdapter.getCount()'

I know matches(withText("Sent . ") doesn't work because it is coming from server and I cannot hardCode delivery timeStamp.

onData(startsWith("Sent .")).inAdapterView(withId(R.id.history)).atPosition(????)
            .onChildView(withId(R.id.timestamp)).

How to do this?

Update:

I tried this and stuck at asserting

final int[] count = new int[1];
    onView(withId(R.id.history))
            .check(matches(new TypeSafeMatcher<View>() {
                @Override
                protected boolean matchesSafely(View item) {
                    ListView listView = (ListView) item;
                    count[0] = listView.getAdapter().getCount();
                    return true;
                }

                @Override
                public void describeTo(Description description) {
                    Log.d("ConversationListTest", "describeTo: " + description);
                }
            }));
onData(containsString("Sent . "))
            .inAdapterView(withId(R.id.history))
            .atPosition(count[0] - 1)
            .onChildView(withId(R.id.timestamp))
            .check(matches(isDisplayed()));

Assertion 'isDisplayed()' gives

 android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id:history'.

Note : not adding 'check(matches(isDisplayed()))' passes my test

What should I use to assert it?


回答1:


I solved by adding this pieces of code

// Below code is to get the view at bottom of the ListView
    final int[] count = new int[1];
    onView(withId(R.id.history))
            .check(matches(new TypeSafeMatcher<View>() {
                @Override
                protected boolean matchesSafely(View item) {
                    ListView listView = (ListView) item;
                    count[0] = listView.getAdapter().getCount();
                    return true;
                }

                @Override
                public void describeTo(Description description) {
                    Log.d("ConversationListTest", "describeTo: " + description);
                }
            }));

// Check if TextView contains "Sent" in it
    onData(anything())        
            .inAdapterView(allOf(withId(R.id.history), isDisplayed()))
            .atPosition(count[0] - 1)
            .onChildView(withId(R.id.timestamp))
            .check(matches(withText(containsString("Sent"))))
            .check(matches(isDisplayed()));



回答2:


Check this tutorial on how to work with Listview - http://www.qaautomated.com/2016/01/testing-with-espresso-data-adapter.html

Try using ListViewClass from code to track the element instead of using ids.

onData(hasEntry(equalTo(ListViewSample.ROW_TEXT),is("List item: 25"))).onChildView(withId(R.id.rowTextView)).perform(click());
onView(withId(R.id.selection_row_value)).check(matches(withText("25")))


来源:https://stackoverflow.com/questions/52151909/android-espresso-how-to-check-value-of-textview-at-the-bottom-of-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!