gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:47:48 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com