-
Notifications
You must be signed in to change notification settings - Fork 21
Example
Mauricio Togneri edited this page Apr 13, 2018
·
12 revisions
This page contains a small example so you can have a glimpse of how to use and what is possible to do with the library.
Given the following feature written in Gherkin:
Feature: Login screen to authenticate users
Scenario: Invalid username and password
Given I see an empty login form
When I introduce an invalid username
And I introduce an invalid password
And I press the login button
Then I see an error message saying 'Invalid credentials'
First, create a class that extends from GreenCoffeeTest
and declare the Activity, the feature and the step definitions that will be used:
@RunWith(Parameterized.class)
public class LoginFeatureTest extends GreenCoffeeTest
{
@Rule
public ActivityTestRule<LoginActivity> activity = new ActivityTestRule<>(LoginActivity.class);
public LoginFeatureTest(ScenarioConfig scenarioConfig)
{
super(scenarioConfig);
}
@Parameters(name = "{0}")
public static Iterable<ScenarioConfig> scenarios() throws IOException
{
return new GreenCoffeeConfig(true) // automatically take a screenshot if a test fails
.withFeatureFromAssets("assets/login.feature")
.scenarios(
new Locale("en", "GB"),
new Locale("es", "ES")
); // the locales used to run the scenarios (optional)
}
@Test
public void test()
{
start(new LoginSteps());
}
}
If no locales are defined, the default one will be used.
Next, create a class containing the steps definitions:
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();
}
}
And that's it, now you can create your own tests using Green Coffee. This is how it looks when you run a more complex test:
You can see an example applied to a full app here.
Next chapter: Installation