I am working on writing a custom Espresso Matcher, but inside
protected boolean matchesSafely(View item)
how to correctly get the child views?
In my case, I want to find the child TextView
with id class_name
and store its String
content to
private String className;
and then try to print this information inside
public void describeTo(Description description)
to get an informative result.
I have tried item.findViewById()
and getChildAt(0)
, but both do not work.
Kindly please help me with this, thanks a lot!
My list_item.xml
is as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/section_header_class_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#267cc5"
android:visibility="gone">
<TextView
android:id="@+id/class_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="8dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/section_header_class_name">
<TextView
android:id="@+id/method_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/test_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp"
android:textSize="@dimen/normal_text"
android:layout_below="@id/method_name"/>
</RelativeLayout>
</RelativeLayout>
My custom ViewMatcher
is as below:
// Match with the TestItemAdapter getView();
static class TestItemMatcher extends TypeSafeMatcher<View> {
private final String mExpectedResult;
private String mActualResult;
private String className;
static TestItemMatcher resultStartsWith(String resultString) {
return new TestItemMatcher(resultString);
}
private TestItemMatcher(String resultString) {
super(TextView.class);
mExpectedResult = resultString;
}
@Override
public void describeTo(Description description) {
description.appendText("Test class: " + className + ", ");
description.appendText("checking the matcher on received view: ");
description.appendText("with expected=\"" + mExpectedResult + "\", but actual=\"" + mActualResult + "\"");
}
@Override
protected boolean matchesSafely(View item) {
RelativeLayout relativeLayout = (RelativeLayout) item;
RelativeLayout childRelativeLayout = (RelativeLayout) relativeLayout.getChildAt(0);
TextView classNameTextView = (TextView)childRelativeLayout.getChildAt(0);
className = classNameTextView.getText().toString();
TextView testResultTextView = item.findViewById(R.id.test_result);
mActualResult = testResultTextView.getText().toString();
return mExpectedResult.equals(mActualResult);
}
}
Aucun commentaire:
Enregistrer un commentaire