Question

I create unit-test from Espresso in Android. My project has NavigationDrawer. I create test which must slide NavigationDrawer and click on button. In understand, how create slide action in my test.

My current solution:

try
    {
        runTestOnUiThread(new Runnable()
        {

            @Override
            public void run()
            {
                DrawerLayout drawer = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
                drawer.openDrawer(Gravity.LEFT);
            }
        });
    }
    catch (Throwable e)
    {
        e.printStackTrace();
    }

But I think it's bad code.

Was it helpful?

Solution

There is a description how to do this in android-test-kit group, explained by ValeraZakharov - link.

OTHER TIPS

Espresso 1.1 has now been out for a little while, but I've still been completely unable to find any Navigation Drawer functionality within it... That is, until I found out that this functionality is actually included within a totally separate library file: espresso-contrib, which is available via gradle with

androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'

This is super briefly mentioned on the "Why Espresso" page of the Espresso website:

If you need any functionality from the contrib library, such as DrawerActions, copy the espresso-contrib jar from here.

Here's the DrawerActions documentation. And here is some sample code that uses it. Hopefully that puts you in the right direction; the EspressoSamples page has no example code for this...

openDrawer() was deprecated, you can use this code instead. I can confirm it works

onView(withId(R.id.my_drawer_layout)).perform(DrawerActions.open());

NavigationDrawer support is coming soon in the next release of Espresso. For the time being, you can implement your own ViewAction, where you would add the code inside your Runnable. This is an incomplete solution because you will likely experience timing issues related to opening/closing of the drawer. Espresso 1.1 will take care of this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top