Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write code for Android Toolbar back button action in Espresso

In my Android application I have Toolbar and need to perform action for tool bar back button click in Espresso. I have tried the following but it does not work

onView(withId(R.id.pageToolbar)).perform(click());

Need to perform its back button click action.

like image 491
M.A.Murali Avatar asked Nov 22 '16 10:11

M.A.Murali


People also ask

How to enable back button in action bar in Android?

How to enable back button in action bar? How to enable back button in action bar? This example demonstrates How to get action bar tittle in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to add back button (arrow) to toolbar in Android?

How to add Back Button (Arrow) to Toolbar 1 Open your Activity class file: Example - MainActivity.java 2 Add the below lines just after super.onCreate (savedInstanceState); in the onCreate () method,#N#package com.example. 3 Now run your App you should see the back arrow button displayed just before the Title. More ...

How to get action bar Tittle in Android?

This example demonstrates How to get action bar tittle in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view. Let's try to run your application.

How to use toolbar in Android Studio?

ToolBar Tutorial With Example In Android Studio 1 Define Design Support Library: To use Toolbar you need to add design support library in build.gradle file. 2 Elements of Toolbar In Android: In Android Toolbar has more features than ActionBar and we can easily replace a ActionBar with Toolbar. 3 Basic Toolbar XML code: More items...


1 Answers

The ContentDescription didn't work for me, so I had to use:

        onView(allOf(
            instanceOf(AppCompatImageButton::class.java), withParent(withId(R.id.toolbar))
        ))
            .perform(click())

because I have not way to identify it univocally in the hierarchy tree:

+-------->AppCompatImageButton{id=-1, visibility=VISIBLE, width=147, height=147, has-focus=false, has-focusable=false, 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=androidx.appcompat.widget.Toolbar$LayoutParams@acd01cf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=21.0, y=0.0}
like image 95
PedroDuran Avatar answered Sep 30 '22 17:09

PedroDuran