安卓Espresso - 如何检查EditText的提示?

27

我开始使用Espresso进行测试,已经运行了基本测试。现在正在尝试找出如何检查我的编辑文本是否具有特定的提示文本?谢谢。

onView(withId(R.id.locationInput)).check(matches ...?)

4个回答

61

自从 Espresso 2.0 开始,只需要使用内部的 ViewMatcher withHint

onView(withId(R.id.locationInput)).check(matches(withHint("your_hint")))

1
这应该是现在被接受的答案——它支持 StringMatcher<String>int(资源 ID)参数。 - David Lord
8
当EditText被包裹在TextInputLayout中时,这种方法不起作用。参考链接:https://code.google.com/p/android/issues/detail?id=191261 - Elye
当EditText包含在TextInputLayout中时,这种方法无法正常工作。调试日志显示hintText为空。 - Sam

20

看起来我想出了解决办法。基本上,你需要创建自己的匹配器:

public static Matcher<View> withHint(final String expectedHint) {
    return new TypeSafeMatcher<View>() {

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof EditText)) {
                return false;
            }

            String hint = ((EditText) view).getHint().toString();

            return expectedHint.equals(hint);
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}

然后您可以使用它:

onView(withId(R.id.locationInput)).check(matches(withHint("Location (Optional)")));

1
您还可以使用BoundedMatcher来避免instanceof检查:https://code.google.com/p/android-test-kit/source/browse/espresso/lib/src/main/java/com/google/android/apps/common/testing/ui/espresso/matcher/BoundedMatcher.java 这里有一个例子:https://code.google.com/p/android-test-kit/source/browse/espresso/lib/src/main/java/com/google/android/apps/common/testing/ui/espresso/matcher/ViewMatchers.java?r=e57a8823a21e05c85a268603d6abe4114fd2dc7c#368 - ValeraZakharov
TypeSafeMatcher已被弃用,那我应该使用什么? - Daniel Gomez Rico
2
org.junit.internal.matchers.TypeSafeMatcher已被废弃,请使用org.hamcrest.TypeSafeMatcher。 - Maurice Gavin

3

有一种略微不同的方法来实现它。在我的情况下,你需要在传递给匹配器之前检查该字符串不为null(如Espresso示例中所述)。并且在下面的代码中,您不需要使用具有此提示的EditText的R.id。您只需检查是否显示了“hintText”提示:

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public final class Matchers {

    public static Matcher<View> withItemHint(String hintText) {
        // use preconditions to fail fast when a test is creating an invalid matcher.
        checkArgument(!(hintText.equals(null)));
        return withItemHint(is(hintText));
    }

    public static Matcher<View> withItemHint(final Matcher<String> matcherText) {
        // use preconditions to fail fast when a test is creating an invalid matcher.
        checkNotNull(matcherText);
        return new BoundedMatcher<View, EditText>(EditText.class) {      

            @Override
            public void describeTo(Description description) {
                description.appendText("with item hint: " + matcherText);
            }

            @Override
            protected boolean matchesSafely(EditText editTextField) {
                return matcherText.matches(editTextField.getHint().toString());
            }
        };
    }
}

使用方法:

import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.isDisplayed;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.your.package.withMatcher.withItemHint;
.
.
.
onView(withItemHint(someHintString)).check(matches(isDisplayed()));

1
我创建了一个 Matcher,支持传递 resourceId 而不是 String。
public static Matcher<View> withHint(final int resourceId) {
     return new BoundedMatcher<View, TextView>(TextView.class) {
     private String resourceName = null;
     private String expectedHint = null;

     @Override
     public boolean matchesSafely(TextView editText) {
        if (null == expectedHint) {
          try {
            expectedHint = editText.getResources().getString(resourceId);
            resourceName = editText.getResources().getResourceEntryName(resourceId);
          } catch (Resources.NotFoundException ignored) {
            /* view could be from a context unaware of the resource id. */
          }
        }

        if (null != expectedHint) {
          return expectedHint.equals(editText.getHint());
        } else {
          return false;
        }
      }

      @Override
      public void describeTo(Description description) {
        description.appendText("with string from resource id: ");
        description.appendValue(resourceId);
        if (null != resourceName) {
          description.appendText("[");
          description.appendText(resourceName);
          description.appendText("]");
        }
        if (null != expectedHint) {
          description.appendText(" value: ");
          description.appendText(expectedHint);
        }
      }
    };
  }

这是一个复制 Espresso 的 withText 匹配器,由 Valera Zakharov 指向 (withText(resourceId)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接