Тест Android Espresso Ui проверить текст метки ActionPage
Я пытаюсь проверить текст ActionPage с помощью эспрессо. Однако, когда я запускаю Средство просмотра автоматизации пользовательского интерфейса, я вижу, что ActionPage отображается как представление вместо ActionView и не имеет TextView.
Я попытался проверить текст ActionLabel, как это, но это не работает:
onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyText")));
у меня есть идентификатор для моей ActionPage, поэтому я могу найти его с onView(withId(R.id.actionPage))
но я не знаю, как получить доступ к своим детям, чтобы получить текст ActionLabel. Я попытался написание пользовательского сопоставления, но это также не работает:
onView(withId(R.id.actionPage)).check(matches(withChildText("MyText")));
static Matcher<View> withChildText(final String string) {
return new BoundedMatcher<View, View>(View.class) {
@Override
public boolean matchesSafely(View view) {
ViewGroup viewGroup = ((ViewGroup) view);
//return (((TextView) actionLabel).getText()).equals(string);
for(int i = 0; i < view.getChildCount(); i++){
View child = view.getChildAt(i);
if (child instanceof TextView) {
return ((TextView) child).getText().toString().equals(string);
}
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText("with child text: " + string);
}
};
}
может кто-нибудь, пожалуйста, помогите мне, ActionLabel, похоже, не имеет id сам по себе и его не TextView...как я могу проверить текст внутри него?
+------>FrameLayout{id=-1, visibility=VISIBLE, width=320, height=320, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+------->ActionPage{id=2131689620, res-name=actionPage, visibility=VISIBLE, width=320, height=320, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-------->ActionLabel{id=-1, visibility=VISIBLE, width=285, height=111, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=17.0, y=209.0}
|
+-------->CircularButton{id=-1, visibility=VISIBLE, width=144, height=144, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=88.0, y=65.0}
1 ответов
можно использовать withParent
С allOf
:
onView(
allOf(
withParent(withId(R.id.actionPage)),
isAssignableFrom(ActionLabel.class)))
.check(matches(withActionLabel(is("MyText"))));
к сожалению ActionLabel
не выставляет свой текст через getText()
, так вместо стандартного withText()
matcher, вы должны написать пользовательский, используя отражение:
private static Matcher<Object> withActionLabel(
final Matcher<CharSequence> textMatcher) {
return new BoundedMatcher<Object, ActionLabel>(ActionLabel.class) {
@Override public boolean matchesSafely(ActionLabel label) {
try {
java.lang.reflect.Field f = label.getClass().getDeclaredField("mText");
f.setAccessible(true);
CharSequence text = (CharSequence) f.get(label);
return textMatcher.matches(text);
} catch (NoSuchFieldException e) {
return false;
} catch (IllegalAccessException e) {
return false;
}
}
@Override public void describeTo(Description description) {
description.appendText("with action label: ");
textMatcher.describeTo(description);
}
};
}
Подробнее: http://blog.sqisland.com/2015/05/espresso-match-toolbar-title.html
пожалуйста, файл ошибку, чтобы запросить Google, чтобы добавить публичный метод ActionLabel.getText()
так что вы можете проверить его без размышлений.