2

我想用Espresso來測試一個帶有顏色選擇器的按鈕,因爲它的背景確實是在用戶按下它時正確改變顏色的。然而,當我嘗試用espresso做這件事時,實際的點擊被註冊得太快以至於按鈕執行它的行爲(啓動一個進度條),並且測試失敗並顯示「view not found」。如何測試在用戶按下按鈕時沒有/在執行按鈕邏輯之前背景顏色是否改變?基本上我在這裏尋找的是一種模擬OnTouchListener的Action_Down的方法。Android:如何使用Espresso測試按鈕bgcolor在按住時是否發生了變化?

Espresso.onView(ViewMatchers.withId(R.id.somebutton)).perform(ViewActions.click()); 

Espresso.onView(ViewMatchers.withText(R.string.somebuttonText)).check(ViewAssertions.matches(withTextColor(Color.BLACK))); 


    public static Matcher<View> withTextColor(final int color) { 
     Checks.checkNotNull(color); 
     return new BoundedMatcher<View, TextView>(TextView.class) { 
      @Override 
      public boolean matchesSafely(TextView warning) { 
       return color == warning.getCurrentTextColor(); 
      } 
      @Override 
      public void describeTo(Description description) { 
       description.appendText("with text color: "); 
      } 
     }; 
    } 

是我得到的錯誤是:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <someid> 

我也試圖做這種方式,它失敗了同樣的錯誤:

ViewInteraction somebuttoninteraction = Espresso.onView(ViewMatchers.withText(R.string.somebuttontext)); 
    somebuttoninteraction.perform(ViewActions.click()).check(ViewAssertions.matches(withTextColor(Color.BLACK))); 

我也試着像這個(longclick而不是click)但仍然相同:

somebuttoninteraction.perform(ViewActions.longClick()).check(ViewAssertions.matches(withTextColor(Color.BLACK))); 

回答

1

解決方案(古怪不夠)是做一些長而曲折的是這樣的:以搜索沒有在點擊後的窗口的前景的看法

Espresso.onView(ViewMatchers.withText(R.string.somebuttontext)).inRoot(RootMatchers.withDecorView(CoreMatchers.is(getActivity().getWindow().getDecorView()))).perform(ViewActions.click()).check(ViewAssertions.matches(withTextColor(Color.BLACK))); 

+0

**無法解析方法'withTextColor(int)'** –

相關問題