Android espresso - 如何检查 Listview 底部 TextView 的值?

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

在上图中,是一个带有TextView(送货报告)的ListView

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

我想检查 'Sent' 条件,这意味着断言消息已成功发送

由于是对话,较新的消息将位于列表视图的底部。

我试过的是...

    // 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 .")));

位置应该是'listAdapter.getCount()'

我知道 matches(withText("Sent . ") 不起作用,因为它来自服务器,我不能硬编码传递时间戳。

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

怎么做?

更新:

我试过了,但坚持断言

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()));

断言'isDisplayed()'给出

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

注意:不添加 'check(matches(isDisplayed()))' 通过了我的测试

我应该用什么来声明它?

查看本教程,了解如何使用 Listview - http://www.qaautomated.com/2016/01/testing-with-espresso-data-adapter.html

尝试在代码中使用 ListViewClass 来跟踪元素,而不是使用 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")))

我通过添加这段代码解决了

// 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()));