Source link:


The guy who serves a great Espresso

Espresso is a great tool to test our Android apps via instrumental tests. With them, we can mimic user actions like clicking a button, scrolling a list, selecting an item on a spinner or swiping on a pager. Then, we can assert that a text appears in the screen, an image is visible or invisible, or a button is enabled or not.

On the other hand, if you tried Espresso, you’ll agree that its API is not discoverable.

Barista introduces a discoverable API for the Espresso features. So, you and all the Android team will write instrumental tests with no effort.

Barista’s Actions API

// Click widgets click(;
  // Click menu items, also overflowed ones clickMenu(;
  // Writing into widgets writeToEditText(, "A great text");
 writeToAutoCompleteTextView(, "Another great text");
  // Select items on AdapterViews clickListViewItem(, 4);
 clickListViewItem(, 4, 5, 6);
 clickRecyclerViewItem(, 2);
 clickRecyclerViewItem(, 2, 3, 4);
 clickRecyclerViewItemChild(, 3,;
 clickRecyclerViewItemChild(, 3, "Button");
  clickSpinnerItem(, 1);
  // Scroll on AdapterViews scrollTo(, 42);
  // Select items on RadioButtons and CheckBoxes clickCheckBoxItem(;
 clickCheckBoxItem("The checkbox text");
 clickRadioButtonItem(, "The radio text");
 clickRadioButtonPosition(, 42);
  // Pick data on pickers setDateOnPicker(1986, 03, 23);
  // Interact with dialogs clickDialogPositiveButton();
  // Scroll on scrolls and pagers scrollTo(;
 scrollTo("A widget with this text");
  // Interact with the navigation drawer openDrawer(;
  // Pull to refresh in SwipeRefreshLayout refresh(;
  // And another tricky feature sleep(2000);
 sleep(2, SECONDS);

Barista’s Assertions API

// Is this view displayed? assertDisplayed("Hello world");
  // ...or not? assertNotDisplayed("Hello world");
  // Is this view enabled? assertEnabled("Hello world");
  // ...or not? assertDisabled("Hello world");
  // Hope this view doesn't exist! assertNotExist("Hello world");
  // Is the expected checkbox checked? assertChecked("Checked checkbox");
  // ...And the other checkbox unchecked? assertUnchecked("Unchecked checkbox");
  // What's the state of the Drawer? assertDrawerIsOpen(;
  // Check EditText's hints assertHint(, R.string.hint);
 assertHint(, "Hint");
  // Check recyclerView item count against expected item count assertRecyclerViewItemCount(, 10);
  // And another tricky feature assertThatBackButtonClosesTheApp();
  // Is this ImageView showing this drawable? assertDrawable(, R.drawable.ic_barista);

Dealing with the runtime permissions dialog

The new Marshmallow permissions system requires checking for permissions at runtime. As Espresso can't interact with the system dialog, Barista offers a way to allow permissions when needed.


Resetting the app's data before running each test

As tests should be isolated, they need to set the environment before running. Espresso doesn't help achieving it but Barista offers a set of rules to clear the app's data before running each test.

@Rule public ClearPreferencesRule clearPreferencesRule = new ClearPreferencesRule();
 // Clear all app's SharedPreferences @Rule public ClearDatabaseRule clearDatabaseRule = new ClearDatabaseRule();
 // Delete all tables from all the app's SQLite Databases @Rule public ClearFilesRule clearFilesRule = new ClearFilesRule();
 // Delete all files in getFilesDir() and getCacheDir()

Dealing with Flaky tests

We should try to write deterministic tests, but when everything else fails Barista helps you deal with flaky tests using a specific ActivityTestRule and a couple of annotations that repeat your tests multiple times.

// Use this rule instead of Espresso's ActivityTestRule @Rule public FlakyActivityTestRule<FlakyActivity> activityRule = new FlakyActivityTestRule<>(FlakyActivity.class, true, false);
  // Use @AllowFlaky to let flaky tests pass if they pass any time. @Test @AllowFlaky(attempts = 5) public void some_flaky_test() throws Exception {

// ... 
  // Use @Repeat to avoid flaky tests from passing if any repetition fails. @Test @Repeat(times = 5) public void some_important_test() throws Exception {

// ... 

One rule to rule them all

All previous rules can be added at the same time by just adding the BaristaRule.

@Rule public BaristaRule<MyActivity> baristaRule = BaristaRule.create(MyActivity.class);
  //...  baristaRule.launchActivity();

The rule assumes some sane defaults:

  • Retry flaky tests: 10 attempts
  • Launch activity automatically: false
  • Initial touch mode enabled: true
  • Clear preferences
  • Clear databases
  • Clear files

Magic that Barista does for you

In order to speed up testing, Barista keeps in mind some considerations.

  • Scrolls when needed: Interacting with Espresso in a ScrollView requires you to scroll to each view, which sometimes doesn't work the first time. Also trying to scroll outside a ScrollView produces an Exception, forcing you to change the test depending on the layout. To keep tests simpler, Barista scrolls automatically before interacting with any View, and only does it if needed.
  • Scrolls on all views: Barista scrolls on all scrollable views, including NestedScrollView. Espresso only handles ScrollView and HorizontalScrollView, so people need to open questions on StackOverflow like this. Or... just use Barista.
  • Just interacts with displayed Views: Interacting with Views inside a ViewPager throws AmbiguousViewMatcherException, because the views you interact with will be potentially repeated on different pages. Barista only interacts with displayed widgets, so you can focus on the behavior instead of wasting time on details.


Include the Google Maven repository, required by Espresso 3:

repositories {

  maven {
 url "" 

Finally just import Barista as a testing dependency:

androidTestCompile('com.schibsted.spain:barista:1.8.0') {

exclude group: '' 

Barista already includes espresso-core and espresso-contrib. If you need any other Espresso package you can add them yourself.


Copyright 2017 Schibsted Classified Media Spain S.L.
Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 


Easily notify a user with a simple alert, inform them of an optional update, and in dire situations block the user from accessing older versions of the application completely.

A Gradle plugin that helps facilitate Github PR checking and automatic commenting of violations.

Leanback cards is a Widget library for use with Android TV applications. It provides an extended set of customisable content cards to enhance your User Interfaces.

This project aims at creating an animation library for different types of loading animations, progress dialogs, etc.

FromTo is a helper library that makes it simple to handle asynchronous actions with views for Android and Java/Kotlin.

Animated icons implemented using AnimatedVectorDrawable.


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes