How to install Espresso and Cucumber in Android Studio

You can download a sample project that is already configured and try first: https://github.com/cucumber/cucumber-jvm/tree/master/examples/android/android-studio/Cukeulator

  • The structure of your project should be like this:

On Android View:

Screen Shot 2015-09-12 at 15.14.59

On Project View:

Screen Shot 2015-09-12 at 15.14.46

 

  • Now, open the build.gradle of your app and write these dependencies:
dependencies {
 androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
 androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
 androidTestCompile 'info.cukes:cucumber-android:1.2.4'
 androidTestCompile 'info.cukes:cucumber-picocontainer:1.2.4'
}
  • I had some problems with the version of java, if you have the same problem just update your java or downgrade/upgrade the version of the plugin which is incompatible.
  • In your build.gradle you will need to write more these configs. Change the name of your application and the package of the runner, following the structure of your project and sync your build.gradle file.
android {
 defaultConfig {
 testApplicationId "com.example.azevedorafaela.myapplication"
 testInstrumentationRunner "com.example.azevedorafaela.myapplication.test.
Instrumentation"
 }
   sourceSets {
     androidTest {
        assets.srcDirs = ['src/androidTest/assets']
     }
   }
} 
  • In your Feature file you an write your scenario like this:
Feature: Test

Scenario: Espresso with cucumber test
Given I have my app configured
When something happens
Then I should see xx on the display
  • In your Instrumentation class:
package com.example.azevedorafaela.myapplication.test;
import android.os.Bundle;
import android.support.test.runner.MonitoringInstrumentation;
import cucumber.api.android.CucumberInstrumentationCore;
public class Instrumentation extends MonitoringInstrumentation {
private final CucumberInstrumentationCore instrumentationCore = new 
CucumberInstrumentationCore(this);
@Override
 public void onCreate(final Bundle bundle) {
 super.onCreate(bundle);
 instrumentationCore.create(bundle);
 start();
 }
@Override
 public void onStart() {
 waitForIdleSync();
 instrumentationCore.start();
 }
}
  • In your Steps file:
package com.example.azevedorafaela.myapplication.test;

import android.test.ActivityInstrumentationTestCase2;

import com.example.azevedorafaela.myapplication.MainActivity;

import cucumber.api.CucumberOptions;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import com.example.azevedorafaela.myapplication.R;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.
matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@CucumberOptions(features = "features")
public class MainActivitySteps extends ActivityInstrumentationTestCase2
<MainActivity> {

    public MainActivitySteps(){
        super(MainActivity.class);
        assertNotNull(getActivity());
    }

    @Given("^I have my app configured$")
    public void I_have_my_app_configured() {
    }

    @When("^something happens$")
    public void something_happens(final char op) {

    }

    @Then("^I should see xx on the display$")
    public void I_should_see_xx_on_the_display(final String s) {
    }
}

 

Now you can start write your espresso code inside of each step. To run your test you need to:

  • In your terminal, open your project folder and run:
gradle --parallel :app:assembleDebugTest
  •  Now you need istall the apk in your device/simulator:
adb install -r app/build/outputs/apk/app-debug.apk
  • Check if your app is installed. Should display the instrumentation of your app
adb shell pm list instrumentation
  • To run the tests with gradle:
gradle connectedCheck
  • To run the tests with adb:
adb shell am instrument -w com.example.azevedorafaela.myapp
lication/com.example.azevedorafaela.myapplication.test.Instrum
entation
  • To run your tests via android configurations:
    1. Open your run configurations
    2. Create an androidTests
    3. Give a name to this config
    4. Select your module
    5. Don’t write anything on the instrumentation field. (We already configured this in our build.gradle)
    6. Ok.

Screen Shot 2015-09-12 at 15.39.22

 

Now you can run your cucumber with espresso tests. I hope this “tutorial” helps you as helped me to install everything on my project.

Thank you guys ! See you next week 🙂

4 thoughts on “How to install Espresso and Cucumber in Android Studio

  1. Hi rafael, I tried something on the similar lines, but, getting the following exception

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ‘:app:preDexDebugAndroidTest’.
    > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java” finished with non-zero exit value 1

    Do you have any idea why this is happening?

    1. Hi Bindu, sorry the delay to reply to you, I was on holidays… So, I had the same problem and I took ages to figure out that is because of the incompatible versions between the plugins. Just have a look what is the version of your Espresso and Cucumber-JVM and try with another versions 🙂

  2. Hi Rafaela,

    Thank you for sharing this information. What I don’t understand in your tutorial is the last step. I mean, I know what are run/debug configurations, but I don’t understand why do you add a new configuration for Android Tests and not for Cucumber.

    Lets say I add that Android Tests configuration. Then I open a .feature file, try to run a Scenario… and I can’t select that run/debug configuration because Android Studio only shows Cucumber configurations. Can you please make it more clear to me?

    Thank you!

    1. Hi Andre, when I wrote this tutorial the Cucumber was not integrated on Android Studio 😦 So, I had to set everything with Android Tests, but you can set the configurations using Cucumber as well… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.