简体   繁体   English

Android Espresso - 关闭 SearchView

[英]Android Espresso - Closing the SearchView

I'm working on Espresso tests for Android and for one of my actions I want to search a list using a SearchView, select an item and then navigate back to the previous screen (by closing the searchview and then pressing the up button).我正在为 Android 进行 Espresso 测试,对于我想使用 SearchView 搜索列表的一项操作,选择一个项目,然后导航回上一个屏幕(通过关闭搜索视图,然后按向上按钮)。

I've tried some things to access the up/collapse button of the SearchView to close it (that don't work):我尝试了一些方法来访问 SearchView 的向上/折叠按钮以关闭它(不起作用):

  • onView(withContentDescription(R.string.abc_action_bar_up_description))
  • onView(withId(R.id.search_close_btn))
  • onView(withId(R.id.home))
  • onView(withId(R.id.homeAsUp))
  • onView(withId(R.id.collapseActionView))
  • Espresso.pressBack() (to just navigate back) Espresso.pressBack() (只是导航回来)

I'm hoping someone might know some way to access and close this button, because I have no idea.我希望有人可能知道某种方式来访问和关闭这个按钮,因为我不知道。

Thanks in advance.提前致谢。

Apparantly I had to press the back button twice to navigate out of it, so if anyone would like to know, you need to use Espresso.pressBack();显然我必须按两次后退按钮才能离开它,所以如果有人想知道,你需要使用Espresso.pressBack(); twice to get out of the search menu.两次退出搜索菜单。

正确的 id 是 R.id.search_close_btn

onView(withId(R.id.search_close_btn)).perform(click())

I was with the same problem of accessing this up/collapse button and by reading the hierarchy of components I found out that this button has the content description of 'Collapse'.我在访问这个向上/折叠按钮时遇到了同样的问题,通过阅读组件的层次结构,我发现这个按钮的内容描述为“折叠”。 So using the following solved the problem for me:所以使用以下解决了我的问题:

onView(withContentDescription(androidx.appcompat.R.string.abc_toolbar_collapse_description)).perform(click())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM