Android Espresso Ui 测试验证 ActionPage 的标签文本
Android Espresso Ui Test verify label text of ActionPage
我正在尝试使用 Espresso 测试 ActionPage 的文本。但是,当我 运行 Ui 自动化查看器时,我可以看到 ActionPage 显示为视图而不是 ActionView,并且它没有 TextView。
我试过像这样检查 ActionLabel 文本,但这不起作用:
onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyText")));
我有一个 ActionPage 的 ID,所以我可以用 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}
您可以将 withParent
与 allOf
一起使用:
onView(
allOf(
withParent(withId(R.id.actionPage)),
isAssignableFrom(ActionLabel.class)))
.check(matches(withActionLabel(is("MyText"))));
不幸的是 ActionLabel
不会通过 getText()
公开其文本,因此您必须使用反射编写自定义匹配器,而不是标准 withText()
匹配器:
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 添加一个 public 方法 ActionLabel.getText()
以便您可以在没有反射的情况下进行测试。
我正在尝试使用 Espresso 测试 ActionPage 的文本。但是,当我 运行 Ui 自动化查看器时,我可以看到 ActionPage 显示为视图而不是 ActionView,并且它没有 TextView。
我试过像这样检查 ActionLabel 文本,但这不起作用:
onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyText")));
我有一个 ActionPage 的 ID,所以我可以用 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}
您可以将 withParent
与 allOf
一起使用:
onView(
allOf(
withParent(withId(R.id.actionPage)),
isAssignableFrom(ActionLabel.class)))
.check(matches(withActionLabel(is("MyText"))));
不幸的是 ActionLabel
不会通过 getText()
公开其文本,因此您必须使用反射编写自定义匹配器,而不是标准 withText()
匹配器:
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 添加一个 public 方法 ActionLabel.getText()
以便您可以在没有反射的情况下进行测试。