android


Espresso - Clicking menu item with app:showAsAction=“ifRoom”


I have the following menu item that I want to click using Espresso:
<item
android:id="#+id/action_save"
android:icon="#drawable/vector_image_save"
android:orderInCategory="4"
android:title="#string/menu_action_save"
app:showAsAction="ifRoom"/>
Due to ifRoom, in some devices the menu is shown as an icon in action bar while in smaller devices it's shown under the "more options".
I could use the below code to tap on the Save icon in the action bar:
onView(withId(R.id.action_save)).perform(click());
And I could use the below code to tap on Save if it's present under the "more options":
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
onView(withText(R.string.menu_action_save)).perform(click());
I want a single test method that would work in both cases.
try {
onView(withId(R.id.action_save)).perform(click());
} catch (NoMatchingViewException e) {
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
onView(withText(R.string.menu_action_save)).perform(click());
}
You will first check if the menu is present as an icon. If so, click on it.
If not, open up the "More options" menu and then select the "Save" menu from the popup.
Note: I know that we shouldn't use conditions in test methods, but I really couldn't find any other solution. If any of you do find a better solution, please post it here.

Related Links

Reuse RecyclerView method onBindViewHolder in pop up menu
how to return link from database by cursor android
ConstraintLayout aspect ratio
Bottom Up drawer in react native [closed]
How to find the touched and relased positions of a view
RecyclerView: No adapter attached; skipping layout in Recycler Android
The tablayout custom font doesn't change
Deleting order, SQLiteDatabase
Unable to generate POJO(model) class using Gson format
Data fetched from FirebaseDatabase is getting shown in 3 separate AlertDialogs instead of one
Alarm not waking phone
Pop up window from menu overflow item selected android
Change in Android Studio Performance
How to call activity on click parent item in expandable listview
How to log all HTTP(S) traffic between Xamarin emulator and the Internet
Unable to find system image for mobile in API 25 (Android 7.1.1)

Categories

HOME
batch-file
facebook-oauth
package
jsf-2.2
sublimetext
alsa
emscripten
tail
babeljs
jetty
google-admin-sdk
retrofit2
webix
title
categorical-data
project-intu
opengl-es-3.0
openbugs
screen-readers
dynamic-sql
easyphp
video-editing
xmonad
googlebot
href
dataflow-diagram
licensing
google-earth
panoramas
gitlab-api
memory-address
gnat-gps
reactjs.net
html-encode
graphhopper
flatmap
android-augmented-reality
jvmti
hackintosh
kill
comparison-operators
outputcache
drupal-theming
mesosphere
xbmc
insight
oracleclient
opengl-4
monkey
gpg-signature
gameplay-kit
tinkerpop3
i386
dimension
simevents
nservicebus5
kango-framework
sciruby
roadkill-wiki
coda
liquid-layout
airbrake
nsurlsessiondatatask
bufferedinputstream
kubuntu
ejb-2.x
delayed-execution
ssis-data-flow
metatrader5
genymotion-call
human-computer-interface
ignite-ui
maven-scm
android-audiorecord
multitasking
spring-3
time.h
mantle
server-administration
dopostback
android-loadermanager
fragmenttransaction
monotouch.dialog
virtualpathprovider
jquery-address
gobject-introspection
user-tracking
http-daemon
tmx
django-paypal
ie-compatibility-mode
instantiationexception
silent
google-wave
firefox-3
soappy
commercial-application

Resources

Encrypt Message