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

时间:2018-09-03 14:41:20

标签: android automation android-espresso

Please see this image

在上图中,它是具有TextView(交付报告)的ListView

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

我要检查“已发送”条件,这意味着断言消息已成功发送

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

我尝试过的是...

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

我知道matchs(withText(“ Sent。”)不起作用,因为它来自服务器,并且我无法硬编码交付timeStamp。

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()))'通过了我的测试

我应该用什么来断言它?

2 个答案:

答案 0 :(得分:0)

查看有关如何使用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")))

答案 1 :(得分:0)

我通过添加以下代码来解决

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