Skip to content
Nico Küchler edited this page Mar 31, 2016 · 13 revisions

Introduction

Espresso Macchiato: A dollop of foamed milk with the texture of melted ice cream.

Main goal is to hide most of the hamcrest style checks and actions behind a fluent style. The hamcrest style is not easy to read and not very intuitive to use compared with a fluent style. So the learning phase for testing with espresso may take a long time. Specially when you miss some experience with hamcrest, espresso and common android layouts (NavigationView, ActionBar).

Support Page Object Pattern: The current visible screen is called a page. Each page has specific elements (Drawer, Button, List, etc). And each element has different actions (click, assert, scroll, etc). For more explanation see http://martinfowler.com/bliki/PageObject.html

Second goal is to provide some useful tools for testing. Tools like clear your application data, prepare and mock contacts, handle permissions, etc.

Pure Espresso or Espresso Macchiato

Simple example test protocol for login process:

  • Login button is initial disabled
  • User type his name
  • User type wrong password
  • User click login button
  • Error message is shown

Test done with standard Android Espresso ...

onView(withId(R.id.buttonConfirm)).check(isDisabled()));
onView(withId(R.id.editTextUsername)).perform(typeText("MyUserName"));
onView(withId(R.id.editTextPassword)).perform(typeText("*****"));
onView(withId(R.id.buttonConfirm)).perform(click()));
onView(withId(R.id.textViewErrorMessage)).check(matches(withText("Username or password not correct.")));

Test done with Espresso Macchiato ...

EspButton.byId(R.id.buttonConfirm).checkIsDisabled();
EspEditText.byId(R.id.editTextUsername).replaceText("MyUserName");
EspEditText.byId(R.id.editTextPassword).replaceText("*****");
EspButton.byId(R.id.buttonConfirm).click();
EspTextView.byId(R.id.textViewErrorMessage).checkTextIs("Username or password not correct.");

Test done with Page Object Pattern ...

loginPage.confirm().assertIsDisabled();
loginPage.username().replaceText("MyUserName");
loginPage.password().replaceText("*****");
loginPage.confirm().click();
loginPage.errorMessage().assertTextIs("Username or password not correct.");

Page Object Pattern with Espresso Macchiato ...

public class LoginPage extends EspPage {

    public EspButton confirm() {
        return EspButton.byId(R.id.buttonConfirm);
    }

    public EspEditText username() {
        return EspTextView.byId(R.id.textViewUsername);
    }

    public EspEditText password() {
        return EspTextView.byId(R.id.textViewPassword);
    }

    public EspTextView errorMessage() {
        return EspTextView.byId(R.id.textViewErrorMessage);
    }
}