Check the font size, height, and width of the EditText with Espresso
Asked Answered
S

2

7

How could I check the font size, height, and width of an EditText with Espresso?

At the moment to sect the text I use:

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

And to read the text:

onView(withId(R.id.editText1)).check(matches(withText("Amr")));
Stamford answered 20/11, 2017 at 18:20 Comment(0)
C
6

You will have to create your own custom matchers since Espresso does not support any of these matchers by default.

Luckily this can be done quite easily. Take a look at this example for font size:

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);
    }

}

Then create an entrypoint like so:

public static Matcher<View> withFontSize(final float fontSize) {
    return new FontSizeMatcher(fontSize);
}

And use it like this:

onView(withId(R.id.editText1)).check(matches(withFontSize(36)));

For width & height it can be done in a similar manner.

Celie answered 29/11, 2017 at 8:54 Comment(0)
B
2

Matcher for view size

public class ViewSizeMatcher extends TypeSafeMatcher<View> {
    private final int expectedWith;
    private final int expectedHeight;

    public ViewSizeMatcher(int expectedWith, int expectedHeight) {
        super(View.class);
        this.expectedWith = expectedWith;
        this.expectedHeight = expectedHeight;
    }

    @Override
    protected boolean matchesSafely(View target) {
        int targetWidth = target.getWidth();
        int targetHeight = target.getHeight();

        return targetWidth == expectedWith && targetHeight == expectedHeight;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("with SizeMatcher: ");
        description.appendValue(expectedWith + "x" + expectedHeight);
    }
}

using

onView(withId(R.id.editText1)).check(matches(new ViewSizeMatcher(300, 250)));
Brantbrantford answered 8/4, 2019 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.