使用 Espresso 检查 EditText 的字体大小、高度和宽度

Rob*_*iro 7 android android-edittext android-espresso

如何使用 Espresso 检查 EditText 的字体大小、高度和宽度?

目前我使用的文本是:

onView(withId(R.id.editText1)).perform(clearText(), typeText("Amr"));

并阅读文本:

onView(withId(R.id.editText1)).check(matches(withText("Amr")));
Run Code Online (Sandbox Code Playgroud)

Sam*_*ekl 5

您必须创建自己的自定义匹配器,因为 Espresso 默认不支持任何这些匹配器。

幸运的是,这可以很容易地完成。看看这个例子的字体大小:

public class FontSizeMatcher extends TypeSafeMatcher<View> {

    private final float expectedSize;

    public FontSizeMatcher(float expectedSize) {
        super(View.class);
        this.expectedSize = expectedSize;
    }

    @Override
    protected boolean matchesSafely(View target) {
        if (!(target instanceof TextView)){
            return false;
        }
        TextView targetEditText = (TextView) target;
        return targetEditText.getTextSize() == expectedSize;
    }


    @Override
    public void describeTo(Description description) {
        description.appendText("with fontSize: ");
        description.appendValue(expectedSize);
    }
Run Code Online (Sandbox Code Playgroud)

}

然后像这样创建一个入口点:

public static Matcher<View> withFontSize(final float fontSize) {
    return new FontSizeMatcher(fontSize);
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

onView(withId(R.id.editText1)).check(matches(withFontSize(36)));
Run Code Online (Sandbox Code Playgroud)

对于宽度和高度,它可以以类似的方式完成。