当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《AndroidEspressoUi测试验证ActionPage的标签文本》经验,为你挑选了1个好方法。

我正在尝试使用Espresso测试ActionPage的文本.但是,当我运行Ui Automation Viewer时,我可以看到ActionPage显示为View而不是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 withChildText(final String string) {
        return new BoundedMatcher(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> chiuki..:

你可以用withParentallOf:

onView(
    allOf(
      withParent(withId(R.id.actionPage)),
      isAssignableFrom(ActionLabel.class)))
  .check(matches(withActionLabel(is("MyText"))));

遗憾的是ActionLabel,不会通过它公开其文本getText(),因此withText()您必须使用反射来编写自定义文本,而不是标准匹配器:

private static Matcher withActionLabel(
    final Matcher textMatcher) {
  return new BoundedMatcher(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()以便您可以无需反思即可对其进行测试.

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有