Create and Configure Cucumber Gradle Project

Create Java Gradle Project using IntelliJ IDEA Community Edition

  1. Open IntelliJ IDEA Community Edition

  2. File >> New >> Project.

  3. From left panel select Gradle and from right panel check the Java

  4. From top of Project SDK it should select Java 13 or your version.

  5. Press Next

  6. From New Project panel

    1. Name: Automation Tutorial (Please put your project name here)

    2. Location: Save location

    3. Click on Artifact Coordinates:

      1. GroupId: com.hmtmcse.tutorial (Please put your domain name reverse)

      2. ArtifactId: java-selenium-cucumber (Please put your id here)

      3. Version: 1.0-SNAPSHOT (Let’s keep it as is.)

  7. Click on Finish

  8. You are successfully created the Gradle project.

Add Cucumber Dependencies and Configuration in Gradle

Package Name com.hmtmcse.tutorial, It’s important for our gradle config, please don’t forget the package name which you put in project creation.

We have to add 3 code blocks into build.gradle:

First

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

Second: Add the cucumber dependency

project.ext {
    cucumberVersion = '5.6.0'
}

dependencies {
    testImplementation 'io.cucumber:cucumber-java:' + cucumberVersion
}

Third: Make sure you replaced the package name com.hmtmcse.tutorial

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'html:reports/test-report', '--plugin', 'pretty', '--glue', 'com.hmtmcse.tutorial', 'src/test/resources']
        }
    }
}

Cucumber Options/Args

  1. --plugin/-p: Specify the plugins which we want to add in project

    1. pretty: It’s generate pretty report.

    2. html: It means, it will generate the html types report in specific location.

  2. --glue/-g: It’s specify project package name, and location of feature file(s)

How to find the build.gradle?

  1. In your IntelliJ IDEA left side, find the panel named Project

  2. Click on it and then look the files, you must will get the build.gradle.

  3. You can also use shortcut, from keyboard press ctrl + shift + n

  4. One popup will appear, from the popup select Files tab.

  5. Then write build.gradle press enter, you will get the file.

Example build.gradle with all configuration.

apply plugin: 'java'


repositories {
    mavenCentral()
}

project.ext {
    cucumberVersion = '5.6.0'
}

dependencies {
    testImplementation 'io.cucumber:cucumber-java:' + cucumberVersion
}

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'html:reports/test-report', '--plugin', 'pretty', '--glue', 'com.hmtmcse.tutorial', 'src/test/resources']
        }
    }
}

YouTube Video Tutorial of Gradle Cucumber Project