2016-09-29 5 views
0

私のフラグメント、rattingfragmentのいずれかがクリックを実行する必要がある場合。 そうでない場合は、他の操作を行います。1つのフラグメントが表示されているかどうか(テストUI)

私はこれが私の画面上の断片であることを知っていますか?

Android UIテスト。 EDIT CODEテストするために私のコードの下 :

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

    @After 
    public void setUp(){ 
     Logout(); 
    } 
    @Test 
    public void shouldOfferRide(){ 
     SystemClock.sleep(1500); 
     Login(); 
     onView(withId(R.id.searchButton)).perform(typeText(emailSearch)); 
     onView(withId(R.id.searchButton)).perform(pressImeActionButton()); 
     onView(withId(R.id.community_user_ask_button)).perform(click()); 
     SystemClock.sleep(1500); 
     onView(withId(R.id.button1)).perform(click()); 
     onView(withId(R.id.send_request)).perform(click()); 
     SystemClock.sleep(1500); 
     ((MainActivity)mActivityRule.getActivity()).navItemClick(4); 
     SystemClock.sleep(3500); 
     LoginMyrides(); 
     onView(withId(R.id.my_rides_tab_bar)).perform(click()); 
     SystemClock.sleep(1000); 

     {I have to know is ratingfragment before perform click below} 

     onView(withId(R.id.rating_bar)).perform(click()); 
     onView(withId(R.id.deny_btn)).perform(click()); 
    } 

     public void Login(){ 
     onView(withId(R.id.edt_new_login_email)).perform(typeText(emailLogin)); 
     onView(withText(R.string.next_button)).perform(click()); 
     onView(withId(R.id.edt_new_password)).perform(typeText(senha)); 
     onView(withText(R.string.login_new_pass)).perform(click()); 
    } 

    public void Logout(){ 
     new SessionManager(mActivityRule.getActivity()).logoutUser(); 
    } 
} 
+0

このケースには多くの解決策がありますが、現在のフラグメントが何であるかを得るためのinstanceOfメソッドが最適です。 if(currentFragmet instanceOf rattingfragment){} – HAXM

+0

http://stackoverflow.com/a/24589081/4961126のように使用する – HAXM

答えて

0

をエスプレッソは、任意の意思決定せずにステップバイステップのレシピに従うことができますテストのために設計されました。フラグメントが表示される状況とそうでない状況を考慮する必要があります。次に、それぞれのシナリオに対して別々のテストを作成します。

0
If you want to know it in activity, then just use: 

Fragment fragment = getFragmentManager().findFragmentByTag(stringTag); 
if (fragment != null) { 
     //here it means the fragment is been shown 
} 

And If you want to know it in fragment then do your stuff in fragment's any of the following life cycle method: 


onAttach(); 
onCreate(); 
onCreateView(); 
onActivityCreated(); 
onStart(); //or 
onResume(); 

これらはすぐにあなたのフラグメントが画面に表示されますと呼ばれます。参考:https://developer.android.com/guide/components/fragments.html#Creating

0

あなたのフラグメントに次の関数を使用します

@Override 
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser); 

    if (isVisibleToUser) { 
     //fragment is visible on screen...do your stuff here 
    } 
    else { 
     // fragment is no longer visible 
    } 
} 
関連する問題