How to Setup a Cucumber Java Project

This is Part 1 of the Cucumber Java Tutorial Series.

Set Up Cucumber

Cucumber is a testing tool used to run automated acceptance tests and is written in BDD (Behavior Driven Development) style. Cucumber is written in Ruby programming language but has later introduced Cucumber JVM of which Cucumber Java is a part. Before we go into Cucumber Java, we will take a quick look at the basic concepts and terminologies in and around Cucumber.

Cucumber Basic Concepts

Test Driven Development (TDD)

Test Driver Development or TDD is the approach where automated test cases are created and executed before developing the actual application. Development involves creating the minimum amount of code to make the failed test cases pass.

Behaviour Driven Development (BDD)

Behaviour Driven Development evolved from TDD with focus to delivery of prioritised, verifiable business value by providing a common vocabulary that reduces the gap between the technical and business sides of the project.

Cucumber

Cucumber is a tool based on Behavior Driven Development (BDD) framework and is written in Ruby. It enables easy readability of the functional validations for technical as well as non technical audience. Tests written in cucumber involve the below three modules.

Feature Files

Feature file is a file with ‘.feature’ extension which describes the feature to test in an easily readable descriptive language. The feature file usually contains a feature with multiple scenarios for testing that feature.

 

Gherkin language

As you can see above, the feature file is written in a particular format known as Gherkin language. Below are the keywords used in it.

  • Feature : Describes the feature to be tested
  • Scenario : Describes the scenario
  • Given : Sets the preconditions
  • When : Describes the action to be performed
  • And : Adds additional condition to above statement
  • Then : Here the validations are performed.
SEE ALSO:   How to Download File using WebDriver

The actual code for performing these actions and validations are written in the step definitions class. We will be covering these in more details in upcoming tutorials in this series.

Cucumber Java Project Setup

Prerequisites for setup

We need JDK and Eclipse installed in our system. In addition to that, we need eclipse plugin for Cucumber which you can get from this update site: http://cucumber.github.com/cucumber-eclipse/update-site

Setup Eclipse Project

  • Create a new Maven Project in Eclipse via New > Maven Project. Select Create a simple Project and Continue. Provide the required details and click Finish. Your project structure would look like this.

ProjectStructure1

  • Open the pom.xml file and select the pom.xml tab to see the xml structure. We have to add dependencies for these jars
    • cucumber-java
    • cucumber-junit: Junit component of cucumber
    • selenium-java: For automating the web functionalities.
    • junit: We will be writing the tests using junit
    • maven-surefire plugin: For easy execution of tests

The pom file should look like this now.

Create your first Cucumber Java project

  • First, we will create our feature file. Create a folder in src/test/resources folder with name ‘features’. Create a file with name ‘login.feature’ inside it.
SEE ALSO:   Setting up Selenium WebDriver Project using TestNG

Cucumber Java Feature

  • Now that we have our feature file, let’s create the Cucumber Runner. Create a class with name ‘CukeRunner’ under package ‘com.axatrikx.steps’ in src/test/java folder.

CreateRunner

  • Add the RunWith and CucumberOptions annotations on the class as shown below
  • The RunWith Cucumber.class tells junit to let Cucumber.class control the execution. CucumberOptions annotations allows you to provide various options.
    • The first features options tells Cucumber to take the feature files from ‘features’ folder in classpath (src/test/resources/features)
    • The ‘pretty’ plugin pretty formats the html and the ‘html’ specifies the report location.
  • Lets run the feature file by right clicking on the Cuke Runner file and select Run As > JUnit Test. This will throw error telling us that the step definitions have not been implemented. It also provides snippets for creating them as well. We just have to copy them into the step definitions class.

FeatureError

  • For creating the step definitions class in ‘com.axatrikx.steps‘ package in src/test/java folder, create an empty class with any name and paste the code snippets in them as shown below.

StepDefinitionsSnippets

  • Next step is removing with PendingExeceptions and coding in the functionality using Selenium WebDriver.
 

  • Now try executing the CukeRunner Junit file again. You will see the test getting executed and the feature passing.
SEE ALSO:   WebDriver Tutorial 1 : Basic Project Setup

Result

 

In the next tutorial, we will cover more about Gherkin grammar and writing more complex Features and Scenarios.

Subscribe to our newsletter to get updates on the upcoming posts.

Automation Testing Tutorials, Tips, Solutions & more once a week


Further Reading

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *