2016-04-22 8 views
3

Ich habe eine list_view, die ich testen möchte.Android Espresso listView scrollen zu

list_view Elementlayout ist RelativeLayout, Adapter ist ItemAdapter.


Dieser Code in Ordnung (Artikel contatining Daft Punk sichtbar ist) funktioniert:

@Test 
public void listViewTest() { 
    Espresso.onView(withText("Daft Punk")).perform(click()); 
} 

Beide dieser Varianten scheitern:

(das Scrollen beginnt, kommt zu dem Punkt und dann schlägt fehl, ist der Fehler sehr und dieser Frage.)

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

artistWithName Definition:

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

Fehler:

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) 

Antwort

4

onData() wird für Sie auf das Element bewegen, nur scrollTo() entfernen und es sollte gut klicken.

scrollTo() ist nur für Produkte verwendet, die im Inneren sind die ScrollView mit onView() kombiniert

+0

Was tun, wenn ich auf ein Element einer Listenansicht blättern wollen? (Es ist noch nicht sichtbar) – iskhakovt

+0

Wenn das Element in der Listenansicht ist, wird onData für Sie blättern. Auch wenn es nicht sichtbar ist. So funktioniert es. Wenn es sich um eine ScrollView handelt, müssen Sie scrollTo() verwenden –