我正在尝试使用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 MatcherwithChildText(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}
你可以用withParent
与allOf
:
onView( allOf( withParent(withId(R.id.actionPage)), isAssignableFrom(ActionLabel.class))) .check(matches(withActionLabel(is("MyText"))));
遗憾的是ActionLabel
,不会通过它公开其文本getText()
,因此withText()
您必须使用反射来编写自定义文本,而不是标准匹配器:
private static Matcher
更多信息:http://blog.sqisland.com/2015/05/espresso-match-toolbar-title.html
请提交一个错误,要求Google添加公开方法,ActionLabel.getText()
以便您可以无需反思即可对其进行测试.