Search code examples
androidcucumberintegration-testingcucumber-jvmcucumber-java

Unable to invoke Cucumber Tests for Android app in Java


I wanted to run cucumber tests. I am unable to invoke or to run these tests. This is what I have done:

In app/build.gradle

defaultConfig {
testApplicationId "com.my.app.test"
testInstrumentationRunner "com.my.app.test.CucumberInstrumentation"
}

sourceSets {
androidTest { assets.srcDirs = ['src/androidTest/assets'] }
}

buildTypes {
debug {
    testCoverageEnabled true
    buildConfigField "String", "TEST_TAGS", "\"${getTestTags()}\""
}
}

def getTestTags() {
    return project.hasProperty("tags") ? project.getProperties().get("tags") : ""
}

dependencies {
    androidTestImplementation 'info.cukes:cucumber-android:1.2.5'
    androidTestImplementation 'info.cukes:cucumber-picocontainer:1.2.5'
}

This is my feature file under the src/androidTest/assets/features directory.

Feature: Enter login details

  @login-feature
  Scenario Outline: Successful Login
    Given   App is launch and user is not logged in
    When    I Click on Log in with Email button
    And Login screen is launched
    And I input an email, "<Email>"
    And I input a password, "<Password>"
    And I press on Log in button
    Then    I Should get logged in and redirect to home screen

    Examples:
      | Email              | Password   |
      | [email protected]     | mypasword123  |

This is my Login StepDefinitions file under the src/androidTest/java/com/my/app/test directory.

class LoginStepdefs {
@Rule
private ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class);

private LoginActivity activity;

@Before("@login-feature")
public void setUp() {
activityTestRule.launchActivity(new Intent());
activity = activityTestRule.getActivity();
}

@After("@login-feature")
public void tearDown() {
activityTestRule.getActivity().finish();
}


@Given("^App is launch and user is not logged in$")
public void appIsLaunchAndUserIsNotLoggedIn() throws Throwable {
System.out.println("appIsLaunchAndUserIsNotLoggedIn");
}

// and other functions
}

Then this is my Runner file.

@CucumberOptions(
features = "features",
glue = "com.my.app.test")
public class CucumberInstrumentation extends MonitoringInstrumentation {
    private final CucumberInstrumentationCore instrumentationCore = new CucumberInstrumentationCore(this);
    @Override
    public void onCreate(Bundle arguments) {
    super.onCreate(arguments);

    String tags = BuildConfig.TEST_TAGS;
    if (!tags.isEmpty()) {
        arguments.putString("tags", tags.replaceAll(",", "--").replaceAll("\\s",""));
    }

    instrumentationCore.create(arguments);
    start();
    }
    @Override
    public void onStart() {
    super.onStart();
    waitForIdleSync();
    instrumentationCore.start();
    }
}

And under AndroidManifest file. i added this

<application
    <uses-library android:name="android.test.runner" />
</application>
<instrumentation
    android:name="cucumber.api.android.CucumberInstrumentation"
    android:targetPackage="com.my.app.test" />

Now I am trying to run the cucumber tests with Android Studio like this:

  • Open “Edit Configuration”
  • Click + on left panel and select “Android Instrumented Tests”
  • Put a name you like to remember with at the Name field on top and select OK.
  • Click “Run”

As a result this is the console output. I have also checked it by using break points, where the debugger never stops.

Testing started at 6:24 PM ...
02/12 18:24:35: Launching CucumberTests
$ adb push /home/sajid/Git/app-android/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/com.my.app
$ adb shell pm install -t -r "/data/local/tmp/com.my.app"
    pkg: /data/local/tmp/com.my.app
Success
APK installed in 2 s 118 ms
$ adb push /home/sajid/Git/app-android/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk /data/local/tmp/com.my.app.test
$ adb shell pm install -t -r "/data/local/tmp/com.my.app.test"
    pkg: /data/local/tmp/com.my.app.test
Success
APK installed in 738 ms
Running tests

$ adb shell am instrument -w -r   -e debug false com.my.app.test/com.my.app.test.CucumberInstrumentation
Client not ready yet..
Started running tests
Tests ran to completion.
Test running failed: Unable to find instrumentation info for: ComponentInfo{com.my.app.test/cucumber.api.android.CucumberInstrumentation}

Empty test suite.

Please guide me where I am going wrong and what I need to make this work.


Solution

  • I don't know what is the reason. But in my feature file

    When I changed "Scenario Outline:" to "Scenario:"

    its start working