怎么能由咖啡android.widget.TextView SETERROR测试?

问题描述 投票:5回答:4

“!无效数据” - 我有password.setError(getResources().getString(R.string.incorrect_data));如果我设置密码无效秀TextView的文本,我需要咖啡来测试它,我写的:

onView(withText(R.string.incorrect_data)).check(matches(isDisplayed()));

但是,这是错误的,我有:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131493034>[incorrect_data] value: Invalid data!
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{307b4f3a IFED.VC. ......ID -480,0-0,1022 #7f0a004c app:id/left_drawer}

如果我写:onView(withText("Invalid data!")).check(matches(isDisplayed()));

我有:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Invalid data!"
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{307b4f3a IFED.VC. ......ID -480,0-0,1022 #7f0a004c app:id/left_drawer}

我用咖啡2:

import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.Espresso.onView;

请帮帮我。

android testing textview android-espresso
4个回答
6
投票

帮助过我:

onView(withId(R.id.password)).check(matches(withError(
                getActivity().getString(R.string.incorrect_data))));

private static Matcher<View> withError(final String expected) {
        return new TypeSafeMatcher<View>() {

            @Override
            public boolean matchesSafely(View view) {
                if (!(view instanceof EditText)) {
                    return false;
                }
                EditText editText = (EditText) view;
                return editText.getError().toString().equals(expected);
            }

            @Override
            public void describeTo(Description description) {

            }
        };
    }

3
投票

我写了咖啡1.x的自定义匹配与之相比对给定值一个TextView的文本颜色。也许你可以采用这种解决方案qazxsw POI的咖啡2.x的

EditText.getError(...)

然后在您的测试案例:

/**
 * Returns a matcher that matches {@link TextView}s based on text property value. Note: View's
 * text property is never null. If you setText(null) it will still be "". Do not use null
 * matcher.
 *
 * @param integerMatcher {@link Matcher} of {@link String} with text to match
 */
public static Matcher<View> withCurrentTextColor(final Matcher<Integer> integerMatcher) {
  checkNotNull(integerMatcher);
  return new BoundedMatcher<View, TextView>(TextView.class) {
     @Override
     public void describeTo(Description description) {
        description.appendText("with text color: ");
        integerMatcher.describeTo(description);
     }

     @Override
     public boolean matchesSafely(TextView textView) {
        return integerMatcher.matches(textView.getCurrentTextColor());
     }
  };
}

/**
 * Returns a matcher that matches {@link TextView} based on it's text property value. Note:
 * View's Sugar for withTextColor(is("string")).
 */
public static Matcher<View> withCurrentTextColor(int color) {
  return withCurrentTextColor(is(color));
}

0
投票

咖啡工作在三个步骤1:找到视图:在视图上执行操作,这将触发要验证3结果的行动:检查视图做它做什么。验证行动或结果与提供的一些断言

onView(withId(R.id.text_warning_title)).check(matches(withCurrentTextColor(activity.getResources().getColor(R.color.black_light))));

通过onView(withId(R.id.password_text)).perform(action); onView(withId(R.id.view_for_expected_outcome)).check(expected outcome) 去看看更多的咖啡。


0
投票

花了一段时间,但这里的Paushchyk Julia的Java代码科特林版本。

https://google.github.io/android-testing-support-library/docs/index.html
© www.soinside.com 2019 - 2024. All rights reserved.