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

标签 android ui-automation android-testing android-espresso

我正在尝试使用 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<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}

enter image description here

最佳答案

您可以将 withParentallOf 一起使用:

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(),以便您可以在不进行反射的情况下对其进行测试。

关于Android Espresso Ui 测试验证 ActionPage 的标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075343/

相关文章:

android - 检测 Android 设备上缺少听筒(仅限免提)

wpf - AutomationProperties.LiveSetting 在 .NET Framework 4.7.1 中的 WPF 中不起作用

Android Studio 模拟器无法使用 API 27

java - 在android中使用Fragment标签时出现XML错误

java - AlarmManager 在第二个间隔完成之前触发

android - 有没有办法从另一个 android 应用程序获取 android 应用程序 View 层次结构?

automated-tests - UI 自动化,在另一个框架内重用代码或破解以使其工作?

android - 特定于测试的应用程序子类

android - 获取android sendevent坐标比例因子

android - callApplicationOnCreate 被调用但不是 newApplication(扩展 AndroidJUnitRunner)