Barista


Source link: https://github.com/SchibstedSpain/Barista

Barista

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(R.id.button);
 click(R.string.button_text);
 click("Next");
 clickBack();
  // Click menu items, also overflowed ones clickMenu(R.id.menu_item);
  // Writing into widgets writeToEditText(R.id.edittext, "A great text");
 writeToAutoCompleteTextView(R.id.autocomplete, "Another great text");
  // Select items on AdapterViews clickListViewItem(R.id.listview, 4);
 clickListViewItem(R.id.listview, 4, 5, 6);
 clickRecyclerViewItem(R.id.recycler, 2);
 clickRecyclerViewItem(R.id.recycler, 2, 3, 4);
 clickRecyclerViewItemChild(R.id.recycler, 3, R.id.button);
 clickRecyclerViewItemChild(R.id.recycler, 3, "Button");
  clickSpinnerItem(R.id.spinner, 1);
  // Scroll on AdapterViews scrollTo(R.id.recycler, 42);
  // Select items on RadioButtons and CheckBoxes clickCheckBoxItem(R.id.checkbox_item);
 clickCheckBoxItem("The checkbox text");
 clickRadioButtonItem(R.id.radiogroup, R.id.radio_item);
 clickRadioButtonItem(R.id.radiogroup, "The radio text");
 clickRadioButtonPosition(R.id.radiogroup, 42);
  // Pick data on pickers setDateOnPicker(1986, 03, 23);
  // Interact with dialogs clickDialogPositiveButton();
 clickDialogNeutralButton();
 clickDialogNegativeButton();
  // Scroll on scrolls and pagers scrollTo(R.id.button);
 scrollTo("A widget with this text");
 swipeViewPagerForward();
 swipeViewPagerForward(R.id.pager);
 swipeViewPagerBack();
 swipeViewPagerBack(R.id.pager);
  // Interact with the navigation drawer openDrawer(R.id.drawer);
 closeDrawer(R.id.drawer);
  // Pull to refresh in SwipeRefreshLayout refresh(R.id.swiperefresh);
  // And another tricky feature sleep(2000);
 sleep(2, SECONDS);

Barista’s Assertions API

// Is this view displayed? assertDisplayed("Hello world");
 assertDisplayed(R.string.hello_world);
 assertDisplayed(R.id.button);
  // ...or not? assertNotDisplayed("Hello world");
 assertNotDisplayed(R.string.hello_world);
 assertNotDisplayed(R.id.button);
  // Is this view enabled? assertEnabled("Hello world");
 assertEnabled(R.string.hello_world);
 assertEnabled(R.id.button);
  // ...or not? assertDisabled("Hello world");
 assertDisabled(R.string.hello_world);
 assertDisabled(R.id.button);
  // Hope this view doesn't exist! assertNotExist("Hello world");
 assertNotExist(R.string.hello_world);
 assertNotExist(R.id.button);
  // Is the expected checkbox checked? assertChecked("Checked checkbox");
 assertChecked(R.string.checked_checkbox);
 assertChecked(R.id.checked_checkbox);
  // ...And the other checkbox unchecked? assertUnchecked("Unchecked checkbox");
 assertUnchecked(R.string.unchecked_checkbox);
 assertUnchecked(R.id.unchecked_checkbox);
  // What's the state of the Drawer? assertDrawerIsOpen(R.id.drawer);
 assertDrawerIsClosed(R.id.drawer);
  // Check EditText's hints assertHint(R.id.edittext, R.string.hint);
 assertHint(R.id.edittext, "Hint");
  // Check recyclerView item count against expected item count assertRecyclerViewItemCount(R.id.recycler, 10);
  // And another tricky feature assertThatBackButtonClosesTheApp();
  // Is this ImageView showing this drawable? assertDrawable(R.id.image_view, 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.

PermissionGranter.allowPermissionsIfNeeded(Manifest.permission.GET_ACCOUNTS);

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.

Download

Include the Google Maven repository, required by Espresso 3:

repositories {

  maven {
 url "https://maven.google.com" 
}
 
}

Finally just import Barista as a testing dependency:

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

exclude group: 'com.android.support' 
}

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

License

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
  http://www.apache.org/licenses/LICENSE-2.0  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. 

Resources

RxJava binding APIs for Android's animations.

Android Wrapper for the Microsoft Project Oxford Emotion API.

A library to make classic bluetooth or BLE easier to use in Android.

Android annotation based SharedPreferences wrapper with fluent interface.

This is a project designed to help controlling Android MediaPlayer class. It makes it easier to use MediaPlayer ListView and RecyclerView. Also it tracks the most visible item in scrolling list. When new item in the list become the most visible, this library gives and API to track it.

Android Timeline is a custom view created to showcase the images against the time photo was taken.

This custom view enables you to simply create a beautiful looking timeline by just including this custom view in your XML file.

Topics


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