我开始尝试Espresso了,基本测试已经开始了。现在想知道如何检查我的编辑文本是否有特定的提示文本?谢谢。
onView(withId(R.id.locationInput)).check(matches...?)
发布于 2013-12-03 02:22:18
看起来我想通了。基本上,你需要创建你自己的匹配器:
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-19 21:08:47
有一种略微不同的方法。在我的例子中,在将字符串传递给matcher之前,您需要检查字符串是否不为空(如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()));
发布于 2014-07-02 19:36:09
我已经创建了一个匹配器,它支持传递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匹配器的复制品。
https://stackoverflow.com/questions/20334455
复制相似问题