Step definitions

Mauricio Togneri edited this page Sep 3, 2016 · 18 revisions

A step definition class must extend from GreenCoffeeSteps and its goal is to declare the methods that will be used to match the steps in the scenarios involved in the test.

Example

public class LoginSteps extends GreenCoffeeSteps
{
    @Given("^I see an empty login form$")
    public void iSeeAnEmptyLoginForm()
    {
        onViewWithId(R.id.login_input_username).isEmpty();
        onViewWithId(R.id.login_input_password).isEmpty();
    }

    @When("^I introduce an invalid username$")
    public void iIntroduceAnInvalidUsername()
    {
        onViewWithId(R.id.login_input_username).type("guest");
    }

    @When("^I introduce an invalid password$")
    public void iIntroduceAnInvalidPassword()
    {
        onViewWithId(R.id.login_input_password).type("1234");
    }

    @When("^I press the login button$")
    public void iPressTheLoginButton()
    {
        onViewWithId(R.id.login_button_doLogin).click();
    }

    @Then("^I see an error message saying 'Invalid credentials'$")
    public void iSeeAnErrorMessageSayingInvalidCredentials()
    {
        onViewWithText(R.string.login_credentials_error).isDisplayed();
    }
}

A step definition method has the following template:

@STEP_TYPE_ANNOTATION("REGULAR_EXPRESSION")
public void NAME_OF_THE_METHOD()
{
    // interaction with UI
    // verification of UI state
}

Step type annotation

Methods used as step definitions must be declared using one of the following annotations:

  • Given
  • When
  • Then
  • And
  • But

Be aware that even though a step definition must have only one of them, the annotation itself is not used to match the steps. For example, the following step definition:

@Given("^I see an empty login form$")
public void iSeeAnEmptyLoginForm()
{
    // ...
}

Will match both of the following steps:

Given I see an empty login form
And I see an empty login form

What it counts is that the regular expression declared in the step definition matches the text after the keywords in the steps.

Regular expression

Each of the annotations used to declare a step definition take a string as a parameter. This string represents the regular expression used to match the steps in the scenarios. In order to avoid false positives, a good recommendation is to start all regular expressions with ^ and end them with $.

Method name

You can name your methods as you want, although to be consistent, a good recommendation is to name them based on the regular expression associated. For example:

@Given("^I see an empty login form$")
public void iSeeAnEmptyLoginForm()
{
    // ...
}

Parameters

Step definitions can take parameters based on how their regular expressions are declared. For example:

@When("^I introduce as password (.+)$")
public void iIntroduceAsPassword(String password)
{
    // ...
}

The regular expression associated to the step definition contains (.+). In this way, when the step definition matches the corresponding step in the scenario, it will extract the text captured in the regular expression. Each group matched in the regular expression will be casted and passed as parameter to the step definition. Methods declaring a step definition can take parameters of type: String, all primitive types (and their associated equivalent classes) and DataTable.

Multiline arguments

The Gherkin language allows to define two types of special arguments for steps: DocString and DataTable. If a step contains one of these type of arguments, it will be passed as the last parameter to the matched step definition.

DocString

The special parameter DocString will be passed to the matched method as a String parameter. For example:

Given an email with the content:
    """
    Subject
    ===============
    This is the body of the email. It contains the
    message’s text, images and other data (such as
    attachments)
    """

Will be received as:

@Given("^an email with the content$")
public void anEmailWithTheContent(String email)
{
    // ...
}
DataTable

The special parameter DataTable will be passed to the matched method as a list of TableRow objects. For example:

Given the following credentials:
    | name  | password |
    | admin | 1234     |
    | guest | 5678     |
    | root  | 0000     |

Will be received as:

@Given("^the following credentials$")
public void theFollowingCredentials(List<TableRow> credentials)
{
    for (TableRow row : credentials)
    {
        for (TableCell cell : row.getCells())
        {
            String value = cell.getValue();
        }
    }
}

Next chapter: Espresso support

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.