2017-10-11 107 views
2

找到視圖底部片我有一個包含CoordinatorLayout有兩個孩子一個Fragmant:一個MapView(谷歌地圖API)&一個RelativeLayout,這最後的佈局包含一些按鈕,我想用Espresso測試。無法與咖啡

因此,這裏是我的測試:

@Test 
fun randomButtonTest() { 
    try { 
     Thread.sleep(2000) 
    } catch (e: InterruptedException) { 
     e.printStackTrace() 
    } 

    // Find the button and perform a click 
    val appCompatImageButton = onView(
      allOf(withId(R.id.random_poi), withContentDescription("Random Point of Interest"), 
        withParent(withId(R.id.design_bottom_sheet)), 
        isDisplayed())) 
    appCompatImageButton.perform(click()) 

} 

這裏是我的佈局(我已刪除的知名度所有的佈局相關的參數):

​​

推出我的儀器測試時,我得到以下錯誤:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: kade.com.accio:id/random_poi and with content description: is "Random Point of Interest" and has parent matching: with id: kade.com.accio:id/design_bottom_sheet and is displayed on the screen to the user)

是因爲我的按鈕位於底部工作表?爲什麼我找不到這個觀點?

回答

1

首先你必須加載片段(否則是沒有測試片段),所以有兩種方法

1)使用實例您FragmentManager片段對象和負載像

@Before // you can do it later 
    void setup(){ 
     fragment = ...  
     getActivity().getFragmentManager().beginTransaction().add(id,farment).commit() 
// or activityTestRule.getActivity() 
//   .getSupportFragmentManager().beginTransaction() 

    } 

注意:如果你的意見外出屏幕的話,可以用withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)代替isDisplayed()

+0

這似乎是一個步驟上,謝謝,但我得到了以下錯誤: >錯誤執行「一次點擊」視圖上「動畫或者在目標設備上啓用轉換。 用您共享的內容替換'isDsplayed()'後。 – Christopher

+0

@Christopher確保遵循[設置您的測試環境](https://developer.android.com/training/testing/espresso/setup.html#set-up-environment)到'在您的設備上,在設置>開發人員選項,禁用設置:',並且當您的目標視圖在屏幕上不可見時,您需要使用'withEffectiveVisibility' –