2016-09-26 92 views
2

我想單擊回收站視圖中的菜單項,我有以下代碼。單擊裏面的回收站視圖:濃咖啡

onView(allOf(withId(R.id.section_list),isDisplayed())).perform(RecyclerViewActions.actionOnItem(hasDescendant(withText("ABC")), MyViewAction.clickChildViewWithId(R.id.payment_menu))); 

和MyViewAction看起來是這樣的:

class MyViewAction { 

public static ViewAction clickChildViewWithId(final int id) { 
    return new ViewAction() { 
     @Override 
     public Matcher<View> getConstraints() { 
      return null; 
     } 

     @Override 
     public String getDescription() { 
      return "Click on a child view with specified id."; 
     } 

     @Override 
     public void perform(UiController uiController, View view) { 
      View v = view.findViewById(id); 
      v.performClick(); 
     } 
    }; 
} 
  • 當我測試它,我可以看到滾動發生到正確的位置,但點擊失敗,出現以下錯誤

android.support.test.espresso.PerformException:執行'android.support.test.espresso.contrib.RecyclerViewActions $ Actio時出錯nOnItemAtPositionViewAction @ 2fd64b56'on view'with id:com.em3Agri.operation.debug:id/section_list'。 at android.support.test.espresso.PerformException $ Builder.build(PerformException.java:83) at ......... 引起:java.lang.IllegalStateException:沒有視圖持有者在位置:38 在android.support.test.espresso.contrib.RecyclerViewActions $ ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:290) 在android.support.test.espresso.ViewInteraction $ 1.run(ViewInteraction.java:144)

+0

你已經在設備dev設置中禁用動畫? –

回答

0

我不知道這是否是您遇到的錯誤的根本原因,但您應嚴格遵守matcheraction之間的區別,即

RecyclerViewActions.actionOnItem(
    hasDescendant(withText("ABC")), 
    MyViewAction.clickChildViewWithId(R.id.payment_menu) 
) 

而應被寫成

RecyclerViewActions.actionOnItem(
    <something-that-selects-R.id.payment_menu-in-your-view-item>, 
    click() 
) 

這是什麼something-that-selects-R.id.payment_menu-in-your-view-item實際上是取決於您的視圖的設置,可能很難正確定義,其他方法,所以也許一個(actionOnHolderItem如)可能是更好的適合。這應該也會運行得更快,因爲它可以直接跳轉到特定視圖所標識的視圖,而Matcher<View>版本只能逐項滾動瀏覽您的RecyclerView,並檢查每個新綁定的版本。

0

如何:

onView(withId(R.id.section_list)) 
    .perform(
    RecyclerViewActions.actionOnItem(
     hasDescendant(withId(R.id.payment_menu)), 
     ViewActions.click() 
    ) 
);