Android Espresso Ui测试验证ActionPage的标签文本

Android Espresso Ui测试验证ActionPage的标签文本,android,ui-automation,android-testing,android-espresso,Android,Ui Automation,Android Testing,Android Espresso,我正在尝试使用浓缩咖啡测试ActionPage的文本。但是,当我运行Ui Automation Viewer时,我可以看到ActionPage显示为视图而不是ActionView,并且它没有TextView 我尝试过像这样检查ActionLabel文本,但不起作用: onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyTe

我正在尝试使用浓缩咖啡测试ActionPage的文本。但是,当我运行Ui Automation Viewer时,我可以看到ActionPage显示为视图而不是ActionView,并且它没有TextView

我尝试过像这样检查ActionLabel文本,但不起作用:

onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyText")));
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);
            }
        };
    }
我的ActionPage有一个id,所以我可以通过
onView(withId(R.id.ActionPage))
找到它,但我不知道如何访问它的子项以获取ActionLabel文本。我尝试编写自定义匹配器,但这也不起作用:

onView(withClassName(equalToIgnoringCase("android.support.wearable.view.ActionLabel"))).check(matches(withText("MyText")));
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);
            }
        };
    }

您可以将
与父项一起使用
所有项一起使用

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);
    }
  };
}
带有ActionLabel的私有静态匹配器(
最终匹配器(文本匹配器){
返回新的BoundedMatcher(ActionLabel.class){
@重写公共布尔匹配安全性(ActionLabel标签){
试一试{
java.lang.reflect.Field f=label.getClass().getDeclaredField(“mText”);
f、 setAccessible(true);
CharSequence text=(CharSequence)f.get(label);
返回textMatcher.matches(文本);
}捕获(无此字段例外){
返回false;
}捕获(非法访问例外e){
返回false;
}
}
@覆盖公共无效描述(描述){
description.appendText(“带操作标签”);
textMatcher.Descripto(描述);
}
};
}
更多信息:


请提交一个bug,请求Google添加一个公共方法
ActionLabel.getText()
,这样您就可以在没有反射的情况下测试它了。

谢谢!问题是
ActionLabel
不能分配给
TextView
并且
ActionLabel
没有公共
getText()
方法。
ActionLabel
是您的自定义视图吗?如果是这样,您可以添加一个
getText()
方法,并使用该方法编写一个自定义匹配器。查看博客链接,获取一个示例自定义匹配器。它不是,它是一个android类。只需在视图上尝试
withParent(withId(R.id.actionPage))。检查(匹配项(withText(“MyText”))我已编辑了答案,以包含一个自定义匹配器,以使用反射验证
ActionLabel
。是否可以通过编程方式将内容描述或id添加到至少一个布局中?这样会容易得多