Issue
I am working on an Android Kotlin project. I am adding UI tests written using the Espresso framework into my project. But in my tests, I cannot select the menu item of the options menu within the action bar using withId() method.
Following is how I added the menu into the action bar in the activity.
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.event_list, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_logout -> {
ApplicationController.instance.clearLoginData()
finish()
}
}
return super.onOptionsItemSelected(item)
}
This is the event_list.xml in the menu resource folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_logout"
android:icon="@android:drawable/ic_lock_power_off"
android:title="@string/menu_item_logout"
app:showAsAction="never"/>
</menu>
This is how my test method
@Test
fun itWipesOutLoginDataWhenLogoutMenuItemIsTapped() {
FakeEventService.SCENARIO_UNDER_TEST = 0
this.eventListActivityRule.launchActivity(null)
openActionBarOverflowOrOptionsMenu(ApplicationProvider.getApplicationContext<Context>())
onView(withId(R.id.action_logout)).perform(click())
}
As you can see in the test method, I am selecting the menu item by using withId() method. But when I run the test, I am getting the following error.
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.example.memento:id/action_logout
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:androidx.appcompat.widget.MenuPopupWindow$MenuDropDownListView{2582e16 VFED.VC.. ........ 0,0-686,168}
View Hierarchy:
+>PopupDecorView{id=-1, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(740,98)(686x168) gr=TOP START CENTER DISPLAY_CLIP_VERTICAL sim={state=unchanged} ty=APPLICATION_SUB_PANEL fmt=TRANSLUCENT surfaceInsets=Rect(112, 112 - 112, 112) (manual)
fl=LAYOUT_NO_LIMITS ALT_FOCUSABLE_IM WATCH_OUTSIDE_TOUCH SPLIT_TOUCH HARDWARE_ACCELERATED FLAG_LAYOUT_ATTACHED_IN_DECOR
pfl=WILL_NOT_REPLACE_ON_RELAUNCH LAYOUT_CHILD_WINDOW_IN_PARENT_FRAME}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->PopupBackgroundView{id=-1, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@de56b69, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+-->MenuDropDownListView{id=-1, visibility=VISIBLE, width=686, height=168, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@4044e8f, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+--->ListMenuItemView{id=-1, visibility=VISIBLE, width=686, height=168, 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, layout-params=android.widget.AbsListView$LayoutParams@393b6fa, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+---->AppCompatImageView{id=2131230882, res-name=group_divider, visibility=GONE, width=0, height=0, 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=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@70144ab, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+---->LinearLayout{id=2131230818, res-name=content, visibility=VISIBLE, width=686, height=168, 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, layout-params=android.widget.LinearLayout$LayoutParams@d648c6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+----->RelativeLayout{id=-1, visibility=VISIBLE, width=574, height=76, 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, layout-params=android.widget.LinearLayout$LayoutParams@4a503dd, tag=null, root-is-layout-requested=false, has-input-connection=false, x=56.0, y=46.0, child-count=2}
What is wrong with my code and how can I fix it?
Solution
For somebody who will search an answer here, the easiest way to interact with menu is by using Barista library. It provides methods like clickMenu(R.id.menu_item)
or openMenu()
for easy menu interactions. The click one works even if the item is hidden inside the overflow.
I have example tests with it in my Android UI testing tutorial.
Answered By - lomza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.