Skip to content

Espresso support

Mauricio Togneri edited this page Apr 15, 2018 · 19 revisions

Although you can choose how to interact with the UI once a step definition is invoked, the library includes a set of methods that makes it more readable using Espresso. You can choose to use these helper methods, use directly Espresso or use any other mechanism.

A class that extends from GreenCoffeeSteps will have access to the following methods:

  • onViewWithId(int resourceId)
  • onViewWithId(@IdRes int resourceId, int index)
  • onViewWithText(int resourceId)
  • onViewWithText(@StringRes int resourceId, int index)
  • onViewWithText(String text)
  • onViewWithText(String text, int index)
  • onViewChildOf(@IdRes int parentViewId, int index)
  • withIndex(Matcher<View> matcher, int index)
  • nthChildOf(Matcher<View> parentMatcher, int childPosition)
  • waitFor(long value, TimeUnit timeUnit)

These methods are used to obtain a reference to an object that can perform operations on a UI component. For example:

@Given("...")
public void someMethod()
{
    onViewWithId(R.id.login_input_username);
    onViewWithText(R.string.login_credentials_error);
    onViewWithText("Some text");
}

All these methods return an object of the class ActionableView. This object can perform actions on the view or verify some conditions. Let's see some examples:

@Given("...")
public void someMethod()
{
    ActionableView view = onViewWithId(...);

    // action
    view.click();
    view.doubleClick();
    view.longClick();
    view.type(String text);
    view.clearText();
    view.scrollTo();
    view.swipeUp();
    view.swipeDown();
    view.swipeLeft();
    view.swipeRight();

    // verification
    view.isDisplayed();
    view.isNotDisplayed();
    view.isCompletelyDisplayed();
    view.isEmpty();
    view.isNotEmpty();
    view.isSelected();
    view.isNotSelected();
    view.isChecked();
    view.isNotChecked();
    view.isFocusable();
    view.isNotFocusable();
    view.isClickable();
    view.isEnabled();
    view.isDisabled();
    view.doesNotExist();
    view.hasFocus();
    view.doesNotHaveFocus();
    view.hasErrorText(String text);
    view.contains(Object element);
    view.doesNotContain(Object element);
    hasDrawable();
    doesNotHaveDrawable();
    check(ViewAssertion viewAssertion);
    perform(ViewAction viewAction);
}

If a verification is not fulfilled, an exception will be thrown and the test for the scenario will be marked as failed.

The class GreenCoffeeSteps also provides the following methods:

  • closeKeyboard(): closes the keyboard
  • pressBack(): simulates pressing the back button
  • string(@StringRes int key): returns the string value for the given key
  • locale(): return the current Locale used in the test
  • takeScreenshot(File file): takes a screenshot and stores it in the given file

Lists

All the previous methods are used to act on UI components such as TextView, EditText, CheckBox, Button, etc. However, when it comes to interact with list views, we need to access elements inside of them in a different way.

Let's imagine we have a list view that displays objects of the class Contact. In order to obtain a reference on an element in the list, we have to create a custom matcher:

public class ContactMatcher extends DataMatcher<Contact, String>
{
    public ContactMatcher(int resourceId)
    {
        super(resourceId, Contact.class);
    }
    
    @Override
    public boolean matches(Contact contact, String content)
    {
        return contact.name().equals(content);
    }
}

Then we can use it to obtain an object of the class ActionableData and perform operations on the matched UI component.

@Given("...")
public void someMethod()
{
    DataMatcher<Contact, String> contactMatcher = new ContactMatcher(R.id.contacts_list_view);
    ActionableData data = contactMatcher.with("...");

    data.click();
    data.doubleClick();
    data.longClick();
    data.scrollTo();
}

Permissions

In order to accept permission during the execution of a test, the best option is to use the GrantPermissionRule.

Clone this wiki locally