Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test word style "ITALIC" in Espresso Testing

I am having problems to test display word in ITALIC style. Can someone provide me any example code to display word style? I am using Espresso and JUnit 4 in android studio. I really appreciated your cooperation. Thank you

like image 735
intan Avatar asked Nov 23 '25 22:11

intan


1 Answers

Please try out the following solution. It might work for you. The core idea is considering using custom ViewMatcher for your case.

public static Matcher<View> withItalicStyle(final int resourceId) {
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("has Italic Text with resource" );
        }

        @Override
        public boolean matchesSafely(View view) {
            TextView textView = (TextView) view.findViewById(resourceId);
            return (textView.getTypeface().getStyle() == Typeface.ITALIC);
        }
    };
}

And in you testcase, you can

    onView(CustomMatchers.withItalicStyle(R.id.yourResourceId)).check(isDisplayed());

For tutorial, please check goole samples in https://github.com/googlesamples/android-testing/blob/master/ui/espresso/IdlingResourceSample/app/src/main/java/com/example/android/testing/espresso/IdlingResourceSample/MainActivity.java

like image 189
Wae Avatar answered Nov 25 '25 10:11

Wae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!