Cucumber Setup with Maven

In our Selenium courses, we tend to use simple Java projects for building, executing, and creating reports. However, you may wish to use Maven, which makes projects much easier to manage. So in this course, we show you how to do Cucumber Setup with Maven in Eclipse.

It is to be noted that Maven requires the JDK Not the JRE, so install Java JDK 1.8 if not installed and set the Windows environment variable that points at the JDK folder.

 

Steps for setting up the Maven Project

Step 1: Create a Maven project

In Eclipse choose New Project->Maven Project-> Create a simple project ticked -> Next

Create Maven Project_Cucumber

Give group id (company namespace e.g. com.tequality) and artifact id (this is the project name e.g. Demobdd) you can also supply a name for the project (anything you like) and a version, you can overwrite this with say 1.0 -> Finish

 

Step 2: Configure POM.XML

Navigate to the newly created project folder in Eclipse and open pom.xml (default view will be on the overview tab->go to pom.xml tab to view the XML)

POM.XML file

Create dependencies tags before </project> tag in the pom.xml file:

Add dependencies from maven repository for below:

  1. Cucumber JUnit
  2. Cucumber java
  3. Junit
  4. Selenium

There are two ways to add a dependency

The first way is – put the cursor after open dependencies tag-> hit Ctrl-Space -> Scroll down in the pop-up box to find; insert dependency-> search for each of the dependencies and click ok on each

Or, just open the Maven repository web site https://mvnrepository.com/ and search for the libraries, the Maven web site gives you the POM entries, so you can just copy & paste

 

Pom.xml should look like below

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>

 

Step 3: Surefire & Maven Compiler Plugins

Add maven surefire plugin and maven compiler (used to compile and execute Junit tests) below dependencies:

<build>
   <pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
</plugin>
<plugins>
 <pluginManagement>
<build>

NOTE: Surefire is used for executing Junit tests testing which is why we can’t get it to run feature files directly. They must go through the Junit runner, naming convention is important too (however you can set it up to run specific test classes if you wish but that requires a bit of configuration)

 

Step 4: Create a Project structure

 A Maven project has a src/main/java which is for the java applications you develop, src/test/java is for your unit tests, which is what we will use as the surefire plugin will set the scope to this classpath by default

Project Structure Cucumber

Step 5: Build the project

  1. Go to your project, right-click->Maven->Update Project… (this fetches all the dependencies)
  2. Go to your project, right click-> run as -> Maven clean (removes any old output files)
  3. Go to your project, right click-> run as -> Maven test (compile & execute Junit tests)