RecylerView没有滚动到android测试案例中的最后文本与浓咖啡

时间:2017-03-17 10:16:22

标签: android ui-automation android-espresso

RecylerView包含textView"一些内容"在最后。我无法将我的recylerview滚动到最后显示的文本。我已经尝试了找到的每个命中和试用方法,但无法找到任何解决方案。

我尝试的代码: -

1. onView(withId(R.id.recylerview)).perform(scrollToPosition(13));
//my last text view in recyler view is at position 13
2.onView(withText("Some content")).perform(scrollTo(), click());
//my text is "Some content"

我的问题是我无法用id搜索它,因为Recycler视图包含所有13个项目的相同文本视图。我已经以编程方式设置了文本值。

因此,此代码无法应用: -

onView(withId(R.id.txtview)).perform(scrollTo(), click());

EspressoCode

UiObject2 button9 = device.findObject(By.text("S 5 [None]"));
        button9.click();
        try {
            UiObject srText = new UiObject(new UiSelector().resourceId("com.example.project:id/radio"));
            sText.click();

        }catch(Exception e){

        }
        onView(withRecyclerView(R.id.recylerview).atPosition(13))
                .check(matches((hasDescendant(withText("Some content")))));

        UiObject2 button10 = device.findObject(By.text("S 6 [None]"));
        button10.click();
        try {
            UiObject sensorText = new UiObject(new UiSelector().resourceId("com.example.project:id/radio"));
            sensorText.click();

        }catch(Exception e){

        }

1 个答案:

答案 0 :(得分:0)

您可以在测试中添加this RecyclerViewMatcher文件。然后将此方法添加到您的espresso测试中,以便您可以使用它

public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
        return new RecyclerViewMatcher(recyclerViewId);
    }

最后做

    onView(withRecyclerView(R.id.recyclerview).atPosition(13))
.check(matches((hasDescendant(withText("Some content")))));

所以你的espresso文件看起来像......

public class YouEspressoTest {

    public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
        return new RecyclerViewMatcher(recyclerViewId);
    }

    @Before
    public void setUp() {
        ...
    }

    @Test
    public void testRecyclerView() {
        ...
        onView(withRecyclerView(R.id.recyclerview).atPosition(13))
                .check(matches((hasDescendant(withText("Some content")))));
        ...
    }
}