Android Espresso listView滚动到

isk*_*ovt 6 android listview android-testing android-espresso

我有一个list_view,我想测试一下.

list_view项目布局是RelativeLayout,适配器是ItemAdapter.


此代码工作正常(项目contftining Daft Punk可见):

@Test
public void listViewTest() {
    Espresso.onView(withText("Daft Punk")).perform(click());
}
Run Code Online (Sandbox Code Playgroud)

这两种变体都失败了:

(滚动开始,到达项目然后失败,错误就在这个问题的正确位置.)

@Test
public void listViewTest() {
    Espresso.onData(artistWithName("Imagine Dragons")).perform(scrollTo(), click());
}

@Test
public void listViewTest() {
    Espresso.onData(artistWithName("Imagine Dragons")).
        .inAdapterView(withId(R.id.list_view))
        .perform(scrollTo(), click());
}
Run Code Online (Sandbox Code Playgroud)

artistWithName定义:

public static Matcher<Object> artistWithName(String expectedName) {
    Checks.checkNotNull(expectedName);
    return artistWithName(equalTo(expectedName));
}

public static Matcher<Object> artistWithName(final Matcher<String> itemMatcher) {
    Checks.checkNotNull(itemMatcher);

    return new BoundedMatcher<Object, ArtistItem>(ArtistItem.class) {
        @Override
        public void describeTo(org.hamcrest.Description description) {
            description.appendText("ArtistTime with name: ");
            itemMatcher.describeTo(description);
        }

        @Override
        protected boolean matchesSafely(ArtistItem artistItem) {
            return itemMatcher.matches(artistItem.getName());
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

错误:

android.support.test.espresso.PerformException: Error performing 'scroll to' on view ' displaying data matching: ArtistTime with name: "Imagine Dragons" within adapter view matching: with id: com.example.iskhakovt.yandextest:id/list_view'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at android.support.test.espresso.DataInteraction.perform(DataInteraction.java:130)
at com.example.iskhakovt.yandextest.MainActivityTest.listViewTest(MainActivityTest.java:52)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(view has effective visibility=VISIBLE and is descendant of a: (is assignable from class: class android.widget.ScrollView or is assignable from class: class android.widget.HorizontalScrollView))
Target view: "RelativeLayout{id=-1, visibility=VISIBLE, width=1080, height=420, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=4}"
Further Info: ScrollToAction on a view inside an AdapterView will not work. Use Espresso.onData to load the view.
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Run Code Online (Sandbox Code Playgroud)

Be_*_*ive 6

onData()将滚动到您的项目,只需删除scrollTo()它应该点击就好了.

scrollTo()仅用于ScrollView组合内部的项目onView()