4

複数のビューに一致するIDを持つRecylerViewを実装するフラグメントを使用してViewPagerをターゲット設定するにはどうすればよいですか?ViewPagerのEspresso Recylerviewは複数のビューに一致します

私はViewPagerを持つMainActivityを持っています。 Viewpagerには5つのタブがあります。これら5つのタブでは、RecylerViewsを使ってそれぞれの画像を読み込みます。

RecylerView XMLはさまざまなフラグメントで再利用されるため、Espressoでアクセスすると、不平等なIDが複数のビューに一致します。

<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

私RecylerViewは画像のみをロードし、任意のテキストをロードしませんので、私もwithText("Text here")を行うことはできません。 RecylerViewsにはonData()を使用できません。

@RunWith(AndroidJUnit4.class) 
@LargeTest 
public class ExampleTest { 

    @Rule 
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
      MainActivity.class); 

    Activity activity = mActivityRule.getActivity(); 

    @Test 
    public void ExampleMethod() { 

     // perform a swipe 
     onView(withId(R.id.viewpager)).perform(swipeLeft()); 

     // try to click on one of the recycler view items. 
     // this crashes: 
     onView(withId(R.id.recycler_view)) 
      .perform(RecyclerViewActions.actionOnItemAtPosition(2, click())); 

    } 
} 

だから、私はViewPagerを使用するときに同じ問題に遭遇した、とisDisplayed()句を追加

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.example.app:id/recycler_view' matches multiple views in the hierarchy. Problem views are marked with '****MATCHES****' below.

//リスト異なる階層

View Hierarchy: 

View Hierarchy: 
+>DecorView{id=-1, visibility=VISIBLE, width=720, height=1280, 
    has-focus=false, has-focusable=true, 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=1} 

//... 

答えて

8

エラーがクラッシュし、指摘私にはViewMatcherが解決されました(ただし、エスプレッソのすべてのものと同様に、時々薄片になる可能性があります)。

私が何をしたか
onView(allof(isDisplayed(), withId(R.id.recycler_view))) 
    .perform(RecyclerViewActions.actionOnItemAtPosition(2, click())); 
+0

おかげで、それはうまくいきませんでした。エラー:recycler_viewは画面に表示されます)は、階層内の複数のビューに一致します。 –

+0

isDisplayed()は私の日を保存しました(TabLayoutとは異なるタブのためにいくつかのRecycleViewを持っていました)。 – Shondeslitch

0

、各フラグメントごとに異なるコンテンツの説明を設定することで、エスプレッソはhasContentDescription()withContentDescription()をサポートしています...

関連する問題