2016-09-29 101 views
0

如果我的片段之一,rattingfragment,我必須執行單擊。 如果沒有,請執行其他操作。我如何知道是否顯示一個片段(測試UI)

我怎麼知道這是我的屏幕上的片段?

Android UI測試。 下面我的代碼來測試 編輯 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方法獲取當前片段。像使用它(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 
    } 
} 
相關問題