27

我开始玩 Espresso,开始运行我的基本测试。现在想弄清楚如何检查我的编辑文本是否有特定的提示文本?谢谢。

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

4

4 回答 4

61

由于Espresso 2.0只需使用内部ViewMatcher withHint

onView(withId(R.id.locationInput)).check(matches(withHint("your_hint")))
于 2015-03-11T12:33:45.650 回答
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)")));
于 2013-12-02T18:22:17.940 回答
3

有一种稍微不同的方法可以做到这一点。在我的情况下,您检查字符串是否不为空,然后再将其传递给匹配器(如 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()));
于 2013-12-19T13:08:47.370 回答
1

我创建了一个支持传递 resourceId 而不是字符串的匹配器

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

它是 Valera Zakharov ( withText(resourceId )指出的 Espresso 的 withText 匹配器的复制品

于 2014-07-02T11:36:09.330 回答