Cucumber Maven Eclipse Setup Tutorial

Reading Time 7 minutes

With basic WebDriver and Cucumber script writing tutorials now covered we will look at Cucumber Maven Eclipse setup. We will look at setting up Cucumber using Maven and we will do this through an Eclipse. Cucumber Maven Eclipse setup should lay foundations for many blog tutorials in this course.

01 Cucumber Maven Eclipse Setup

What is Maven? Maven is a build tool which can be used to build projects and manage their dependencies. What this means is that Maven can be used to automatically download jars for a given Java project without the user having to manually download any. It makes it more easier and manageable when building and maintaining projects. To date we have been manually download our jars from websites such as Selenium and Maven Repository but with Maven all of that is taken care of automatically.

02 IDE Installation

What is an IDE? An IDE (Integrated Development Environment) is the technical term given to a product which allows someone to write code or scripts through a user interface. An IDE can also allow the same user to download plugins which allows further customization, for instance some IDE’s do not support Cucumber straight out of the box but with the power of a plugins this can easily be remedied.

Eclipse is perhaps one of the most common IDE’s available to use, it is open source and very famous among both new and senior developers. There are other options such as IntelliJ but in this blog I am picking Eclipse simply because it’s simplicity and ease of use. Let go ahead and download Eclipse from https://eclipse.org/downloads/.

I am going to download ‘Eclipse IDE for Java Developers’. After your download completes then you should notice that the fully downloaded file is a zipped file. This means you will need an ‘unzipping’ program to unzip the file. Eclipse downloads are a little different as the file you have just downloaded is the whole applcation. Sorry if you were expecting an installer but luckily with Eclipse once you unzip the folder all you have to do is copy the unzipped folder to a location of your choosing. I personally will move it to my C: driver. Which ever location you chose, open the folder and start Eclipse by double clicking on the Eclipse application icon.

If Eclipse opens then you have completed your IDE installation. At the time of writing this blog the version of Eclipse IDE for Java Developers is Mars.2 Release (4.5.2).

03 Getting Cucumber to work with Eclipse

The first thing we need to do is install a Cucumber plugin to help us write Feature files for Eclipse although a plugin is strictly not needed to write Feature Files in Eclipse. To add the plugin select ‘Help’ from the main menu and select ‘Install New Software’. Once the window opens click on the ‘Add’ button from the top right and enter the following:

Name: cucumber
Location:  http://cucumber.github.com/cucumber-eclipse/update-site

Click ‘OK’ to add the plugin to your plugin list. Select ‘Cucumber Eclipse Plugin’ from the newly generated list and hit the ‘Next’ button until the plugin is installed. Accept any warnings which may display and once the plugin is installed Eclipse will perform a restart. The version of the plugin at the point of time when writing this blog is ‘0.0.15.201605080636’. This is one of the first steps towards Cucumber Maven Eclipse setup.

With the plugin installed lets go ahead and create a brand new Maven project. To do this select ‘New’ from the top bar menu and select ‘Other’. When the ‘New Wizard’ window opens type in Maven into the filter box and select ‘Maven Project’. This will display the New Maven project windows. Lets go with the default settings for now. When Group Id and Artifact Id are displayed type in com.thetestroom for the Group Id and cucumber_project for the Artifact Id and click ‘Finish’. This should have now created your Maven project.

With the project window opened (if it is not then ensure all other Eclispe windows are closed such as the ‘Welcome’ window) right click on src/test/java/com.thetestroom/cucumber_project and select ‘new’ and then ‘file’. In the file dialog box set the file name to myFirstFeature.feature. Notice that this creates a Feature File which Eclipse recognizes. Copy in the following contents into the Feature File:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the WebDriver Cucumber tutorial main page has loaded
Given I navigate to TheTestRoom.com
When I navigate to Cucumber Tutorial page
Then the page title should be visible

Once done right click inside the Feature File and select ‘Run As’ and then ‘Cucumber Feature’. As you are able to see the option to run a Feature File as Cucumber Feature then this proves that your Eclipse is now configured to run Feature Files. This is great. After running your Feature file you should see the output below:

Error: Could not find or load main class cucumber.api.cli.Main

This means that you don’t have the required files yet for this Cucumber Maven Eclipse setup. To do this we need to add dependencies into the pom.xml file. In previous tutorials we used to copy in the Jars manually but with Maven we don’t have to do any of this since Maven will automatically download the jars and place them in the Classpath. This is what makes Maven so powerful. Copy in the following dependencies below into your pom.xml file:


     info.cukes
     cucumber-jvm-deps
     1.0.5


     info.cukes
     gherking
     2.12.2


     info.cukes
     cucumber-core
     1.2.4


     info.cukes
     cucumber-java
     1.2.4

Now right click on your Featiure file and run it as a Cucumber Feature and the output should be something like this:

Feature: To check that main tutorial course pages have loaded in TheTestRoom.com

Scenario: To check that the WebDriver Cucumber tutorial main page has loaded # C:/Users/MohammedShahin/workspace/cucumber_project/src/test/java/com/thetestroom/cucumber_project/myFirstFeature.feature:3
    Given I navigate to TheTestRoom.com
    When I navigate to Cucumber Tutorial page
    Then the page title should be visible

1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.000s

You can implement missing steps with the snippets below:

@Given("^I navigate to TheTestRoom\\.com$")
public void i_navigate_to_TheTestRoom_com() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}
@When("^I navigate to Cucumber Tutorial page$")
public void i_navigate_to_Cucumber_Tutorial_page() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}
@Then("^the page title should be visible$")
public void the_page_title_should_be_visible() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}  

Great, it looks like we have been able to get Cucumber to now fully work via Eclipse. Only a handful of tasks remain now to complete this Cucumber Maven Eclipse setup. First we need to create the Step Definition file so lets create a new Java class called myFirstStepDefinition under src/test/java/com.thetestroom/cucumber_project/ and copy in the contents as shown below:

package com.thetestroom.cucumber_project;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.java.en.*;

public class myFirstStepDefinition {
    WebDriver driver;

    @Given("^I navigate to TheTestRoom\\.com$")
    public void i_navigate_to_TheTestRoom_com() throws Throwable {
        System. out .println("Going to TheTestRoom.com" );
        driver = new FirefoxDriver();
        driver .navigate().to("http://www.thetestroom.com" );
    }
    @When("^I navigate to Cucumber Tutorial page$")
    public void i_navigate_to_Cucumber_Tutorial_page() throws Throwable {
        System. out .println("Clicking on Cucumber Tutorial Page" );
        driver .findElement(By.linkText( "Java Cucumber Tutorial")).click();
    }
    @Then("^the page title should be visible$")
    public void the_page_title_should_be_visible() throws Throwable {
        System. out .println("Checking page title" );
        System. out .println(driver .getTitle());
        driver .quit();
    }
}

We also need to add in WebDriver (Selenium) as a dependency in our pom.xml file. Add in the dependency below:


     org.seleniumhq.selenium
     selenium-java
     2.53.0

And that’s it. Now run the Feature file on more time. Right click on it and select ‘Run As’ and then ‘Cucumber Feature’. You should now see a Firefox web browser open, navigate to TheTestRoom.com, click on the Cucumber Tutorial link and print out the title of the page.

And that’s it folks, we now have a working Eclipse Cucumber Project. In this tutorial we have learnt how to use Maven to help manage our dependencies and as well as learning how to run Cucumber Features via Eclipse. We have now covered a full tutorial on Cucumber Maven Eclipse setup.

Video Tutorial on YouTube

cucumber maven eclipse setup

Direct Video Link

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.

  • JohnK

    This is a well written tutorial. Thanks.

    • Mo

      Thanks John. It would be great if you can checkout some of my more recent posts.