2016-04-22 61 views
3

私はテストしたいlist_viewを持っています。Android Espresso list scroll to

list_viewアイテムのレイアウトはRelativeLayout、アダプタはItemAdapterです。


このコードは正常に動作します(ダフトパンクをcontatining項目が表示されている):

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

これの両方が失敗したバリアント:

(スクロールが、その後、起動する項目に来て、失敗した場合、このエラーは非常に問題です。)

@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定義:

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

エラー:

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) 

答えて

4

onData()は、あなたのための項目にスクロールだけscrollTo()を削除し、それがうまくクリックする必要があります。

scrollTo()は、私だけがリストビューの項目にスクロールしたい場合に何をすべきかonView()

+0

と組み合わせるScrollView内にあるアイテムのために使用されていますか? (まだ表示されていません) – iskhakovt

+0

アイテムがリストビューにある場合、onDataがスクロールします。たとえそれが見えなくても。これはどのように動作するのですか?それがScrollViewの場合、scrollTo()を使用する必要があります。 –