2017-10-15 16 views
0

私は自分のアプリケーションのテストケースを書く作業をしており、アクティビティでうまくいきました。しかし、ViewPager内のFragmentについては、私のケースは失敗します。Androidのエスプレッソフラグメントとその子供のケースをAcitivtyから書き込む方法は?

これを直接実行するとフラグメントが空のビューを示すため、以下のテストケースは失敗します。

パブリッククラスSentimentsFragmentEspressoTest {

@Rule 
public FragmentTestRule<SentimentsFragment> mFragmentTestRule = new FragmentTestRule<>(SentimentsFragment.class); 

@Test 
public void fragment_can_be_instantiated() { 

    // Launch the activity to make the fragment visible 
    mFragmentTestRule.launchActivity(null); 

    // Then use Espresso to test the Fragment 

    onView(withId(R.id.listSentiments)) 
      .perform(RecyclerViewActions.scrollToPosition(4)); 
} } 

だから、誰がどのように比べてこの問題を来るように私に言うことができますか? おかげで、ViewPager内部断片に対する K.ラジェッシュ

+0

おそらく、あなたが 'launchActivity(null);'を呼び出すので、ビューは空/ヌルになるでしょう。 –

+0

@ H.Brooks、launchActivity()には、Intentパラメーターがあります。断片を送る方法? – itsrajesh4uguys

答えて

0

は私の上記の質問に対する解決策を発見した、あなたのテスト

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

@Before 
public void setUp() throws Exception { 

    //get fragment 
    mActivityRule.getActivity() 
      .getSupportFragmentManager().beginTransaction(); 


} 

@Test 
public void testRecyclerView() { 

    //click on item recyclerview 
    onView(withId(R.id.listSentiments)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click())); 
    .... 
    ... 
+0

運がありません。 ** java.lang.NullPointerException:ヌルオブジェクト参照に仮想メソッド 'android.support.v4.app.FragmentManager com.MyActivity.getSupportFragmentManager()' **を呼び出そうとしました。 – itsrajesh4uguys

0

で使用するようにしてください。コードスニペットは次のとおりです。

ViewInteraction recyclerView = onView(
         allOf(withId(R.id.listSentiments), 
           withParent(withId(R.id.viewpager)), 
           isDisplayed())); 
       recyclerView.perform(actionOnItemAtPosition(3, click())); 

このようにして、Viewpagerで接続されているすべてのフラグメントにアクセスできます。

関連する問題