2017-11-21 16 views
1

내 레이아웃의 일부입니다사용 에스프레소 여기

<com.rey.material.widget.EditText 
      android:id="@+id/tagEditorSettings" 
      android:layout_width="match_parent" 
      android:layout_height="48dp" 
      android:layout_marginStart="10dp" 
      android:layout_toEndOf="@+id/circularProgressbar" 
      android:hint="@string/tag_name_tag_settings" 
      android:inputType="text" 
      android:textSize="18sp" 
      app:et_dividerColor="@color/my_primary" 
      app:et_dividerHeight="1dp" 
      app:et_inputId="@+id/name_input" 
      app:et_labelEnable="true" 
      app:et_labelTextSize="14sp" 
      app:et_supportLines="1" /> 

내 테스트 코드 :

onView(withId(R.id.tagEditorSettings)) 
       .perform(click()).perform(clearText()); 

그리고 나는 그것을 실행하려고하면 나는 그런 오류 얻을 :

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: (is displayed on the screen to the user and is assignable from class: class android.widget.EditText)

"클래스에서 할당 할 수 있습니다 : class android.widget.EditText"로 문제가 있음을 알고 있지만, 누군가가 조언을 해줄 수 있고, 어떻게 고칠 수 있습니까? 내 경우에 사용합니까?

답변

2

문제 : com.rey.material.widget.EditTextFramelayout로부터 연장되고 clearTextReplaceTextActionview, rey...EdiText 이후

allOf(isDisplayed(), isAssignableFrom(EditText.class)); 
//     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

같은 EditText 유형 적용의 서브 클래스가 아닌 곳에 ReplaceTextAction

public static ViewAction clearText() { 
     return actionWithAssertions(new ReplaceTextAction("")); 
    } 

로 사용 EditText 따라서 오류

,

솔루션 : 만들 자신 ViewAction

public static ViewAction clearTextInCustomView(){ 
      return new ViewAction() { 
       @SuppressWarnings("unchecked") 
       @Override 
       public Matcher<View> getConstraints() { 
        return allOf(isDisplayed(), isAssignableFrom(com.rey.material.widget.EditText.class)); 
//           ^^^^^^^^^^^^^^^^^^^ 
// To check that the found view is type of com.rey.material.widget.EditText or it's subclass 
       } 

       @Override 
       public void perform(UiController uiController, View view) { 
        ((com.rey.material.widget.EditText) view).setText(""); 
       } 

       @Override 
       public String getDescription() { 
        return "clear text"; 
       } 
      }; 
    } 

로 그리고 나중에

onView(withId(R.id.tagEditorSettings)) 
       .perform(click()).perform(clearTextInCustomView()); 
+0

너무 감사합니다! 당신은 많은 시간을 절약했습니다!) – JohnA

+0

@JohnA 내가 도울 수있어서 기뻐요 –

1

글고FrameLayout이에서 확장이 사용자 정의 할 수 있습니다. 그래서 타겟이 클래스 android.widget.EditText에서 할당 가능하다고 불평하는 것입니다.

그러나,이 사용자 정의 글고 치기는 다음과 같이 정의된다 포함 된 글고의 텍스트 설정 의 setText 같은 몇 가지 방법이 있습니다

protected android.widget.EditText mInputView; 

을 본 사람은 android.widget 패키지에서 글고입니다.

당신은 상자 사용자 지정 ViewAction이 public 메소드에 액세스 할 수 있습니다

public static ViewAction setTextEditText(final Matcher<View> matcher, 
    final String newText) { 
    return new ViewAction() { 
     @Override 
     public Matcher<View> getConstraints() { 
      return allOf(isDisplayed(), isAssignableFrom(com.rey.material.widget.EditText.class)); 
     } 

     @Override 
     public String getDescription() { 
      return "Update the text from the custom EditText"; 
     } 

     @Override 
     public void perform(final UiController uiController, final View view) { 
      ((com.rey.material.widget.EditText) view).setText(newText); 
     } 
    }; 
} 

그런 다음 실행할 수있는 사용자 정의 ViewAction을 :

onView(withId(R.id.tagEditorSettings)).setTextEditText(null); 
+0

고마워요! 매력처럼 작동) – JohnA