项目作者: mauriciotogneri

项目描述 :
Android library that allows you to run your acceptance tests written in Gherkin in your Android instrumentation tests.
高级语言: Java
项目地址: git://github.com/mauriciotogneri/green-coffee.git
创建时间: 2016-08-22T11:52:37Z
项目社区:https://github.com/mauriciotogneri/green-coffee

开源协议:MIT License

下载


License
Android Arsenal

Green Coffee

Green Coffee is a library that allows you to run your acceptance tests written in Gherkin in your Android instrumentation tests using the step definitions that you declare. Visit the wiki for more detailed information.

Example

Given the following feature:

  1. Feature: Login screen to authenticate users
  2. Scenario: Invalid username and password
  3. Given I see an empty login form
  4. When I introduce an invalid username
  5. And I introduce an invalid password
  6. And I press the login button
  7. 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:

  1. @RunWith(Parameterized.class)
  2. public class LoginFeatureTest extends GreenCoffeeTest
  3. {
  4. @Rule
  5. public ActivityTestRule<LoginActivity> activity = new ActivityTestRule<>(LoginActivity.class);
  6. public LoginFeatureTest(ScenarioConfig scenarioConfig)
  7. {
  8. super(scenarioConfig);
  9. }
  10. @Parameters(name = "{0}")
  11. public static Iterable<ScenarioConfig> scenarios() throws IOException
  12. {
  13. return new GreenCoffeeConfig()
  14. .withFeatureFromAssets("assets/login.feature")
  15. .takeScreenshotOnFail()
  16. .scenarios(
  17. new Locale("en", "GB"),
  18. new Locale("es", "ES")
  19. ); // the locales used to run the scenarios (optional)
  20. }
  21. @Test
  22. public void test()
  23. {
  24. start(new LoginSteps());
  25. }
  26. }

Next, create a class containing the steps definitions:

  1. public class LoginSteps extends GreenCoffeeSteps
  2. {
  3. @Given("^I see an empty login form$")
  4. public void iSeeAnEmptyLoginForm()
  5. {
  6. onViewWithId(R.id.login_input_username).isEmpty();
  7. onViewWithId(R.id.login_input_password).isEmpty();
  8. }
  9. @When("^I introduce an invalid username$")
  10. public void iIntroduceAnInvalidUsername()
  11. {
  12. onViewWithId(R.id.login_input_username).type("guest");
  13. }
  14. @When("^I introduce an invalid password$")
  15. public void iIntroduceAnInvalidPassword()
  16. {
  17. onViewWithId(R.id.login_input_password).type("1234");
  18. }
  19. @When("^I press the login button$")
  20. public void iPressTheLoginButton()
  21. {
  22. onViewWithId(R.id.login_button_doLogin).click();
  23. }
  24. @Then("^I see an error message saying 'Invalid credentials'$")
  25. public void iSeeAnErrorMessageSayingInvalidCredentials()
  26. {
  27. onViewWithText(R.string.login_credentials_error).isDisplayed();
  28. }
  29. }

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:

Example

You can see an example applied to a full app here.

Installation

Add the following code to your root build.gradle:

  1. allprojects
  2. {
  3. repositories
  4. {
  5. maven
  6. {
  7. url 'https://jitpack.io'
  8. }
  9. }
  10. }

Add the following code to your module build.gradle file:

  1. dependencies
  2. {
  3. androidTestImplementation 'androidx.test:runner:1.3.0'
  4. androidTestImplementation 'androidx.test:rules:1.3.0'
  5. androidTestImplementation 'com.github.mauriciotogneri:green-coffee:3.6.0'
  6. }

And the following test instrumentation runner:

  1. defaultConfig
  2. {
  3. testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
  4. }