Cucumber with Java

Introduction:

Cucumber is an open-source, multiple language supportive tool that uses Behavior Driven Development (BDD). With the help of this tool, we can explain the behavior of an application in simple English text using the Gherkin language. The tests written can be understood regardless of one’s technical knowledge. The Cucumber framework contains three parts i.e., Feature File, Step Definitions, and the Test Runner File. The tests in Cucumber are written within a plain text file having ‘.feature’ as an extension. This feature file contains multiple scenarios, and each scenario contains multiple steps. Cucumber can be easily integrated with Selenium, Ruby on Rails, and other web-based testing tools and is widely used for acceptance testing. 

In this blog, we will discuss- 

  • TDD vs BDD vs ATDD 
  • Project Setup for using Selenium and Cucumber framework with java in Eclipse 
  • File Structure for Selenium and Cucumber
  • How to install the cucumber plugin 

Prerequisites:

  • Install Java JDK
  • Download Eclipse

BDD vs TDD vs ATDD: 

BDD:

This technique is an extension of TDD, it allows us to write our test cases in plain English-like sentences. The behavior of an application is explained using simple words and is more user-focused, therefore user stories are created for every functionality of an application. It improves communication between tech and non-tech teams. 

Benefits: Coverage of user stories, clarity of scenarios, reusing of code in framework, parameterized feature file.  

Approach: This technique uses the test first approach, it works on a higher level of abstraction as compared to TDD by defining the behavior in the form of user stories, feature files, and scenarios. It includes the participation of three authors i.e., PO, Developer, and QA. 

Lifecycle:

  1. Write the failure feature scenarios. 
  2. Make the tests pass by making sure to fix the problem.
  3. Refractor the code by making changes in the base code.
  4. Follow the same process from the beginning to keep repeating the cycle n number of times w.r.t to BDD approach.  

TDD: 

Using this technique, we first create the test cases and then proceed with the coding after which the test cases can be executed and passed, it is an iterative development process where every iteration starts with a set of tests written for a new piece of functionality. It instructs to write a new code if an automated test fails. 

Benefits: Code duplication is avoided, code improvement due to refactoring, allows unit test cases to be done easily, and makes the code simple, bug-free and clearer. 

Approach: This technique uses the test first approach, it has an implementation-centric approach, with more unit-level test cases and developers being the main author for these test cases leading to white box testing.  

Lifecycle: 

  1. Execute the test cases against the requirement that we have which means we first need to write a failed test 
  2. Make the changes and make the code work until test cases are passed.  
  3. Improve the base code i.e., to follow the refractor cycle and again follow the same process from the beginning for writing more test cases. 

ATDD: 

In this technique, the whole development team and business stakeholders are involved and it is built on top of TDD. The developer first writes the tests for acceptance criteria and then writes the code to make the test pass, if the test fails, the code is refactored until the test is passed. 

Benefits: The acceptance criteria help testers to easily identify the required testing, it helps developers to write precise unit tests, it reduces defects, and defines acceptance criteria and tests during story creation.  

Approach: This technique has a higher level of abstraction in comparison with TDD and BDD, it mainly focuses on higher-level requirements, and it majorly includes the participation of three authors i.e., PO, BA, and Stakeholders. 

Lifecycle:

  1. The PO and stakeholders define the required documentation or a user story  
  2. Acceptance test cases are written in the form of acceptance criteria  
  3. Acceptance test cases are shared with testers  
  4. The tester will further follow the TDD lifecycle approach which means that after getting the acceptance criteria in the ATDD lifecycle we are following the TDD lifecycle and we can also apply the BDD lifecycle  
  5. The Code is developed with changes made to it 
  6. The demo is given to the client  
  7. The product is delivered to the customer. 

Project Setup for Selenium and Cucumber framework:

Create a Maven project.

To create a maven project, we need to right-click under the package explorer in Eclipse

IDE > go to New> go to Others> select Maven Project option> click on Next

Selenium and Cucumber Maven project

> choose Create a simple project option> click on Next 

Selenium and Cucumber new Maven project

>Provide the New Maven Project Group Id> Provide Artifact Id>click on Finish 

New Maven project

Add the Maven dependencies 

To get the maven dependencies first we need to visit the link below: 

https://mvnrepository.com/repos/central  and then we need to search for the required dependency i.e., Cucumber JVM: Java (Io. cucumber)  

Selenium and Cucumber

Select Cucumber JVM: Java (Io. cucumber) >click on the latest version>copy the dependency 

Selenium and Cucumber

> add it to the pom.xml file 

Selenium and Cucumber POM

>We can check the cucumber java-related dependencies, jar files, and libraries in the Maven Dependencies folder as shown in the below image 

Selenium and Cucumber project explorer

File Structure for Selenium and Cucumber:

Create a folder in src/test/resources.

 To create a folder right click on src/test/resources> go to New> select Folder>Provide the name of the folder>click on Finish.

New folder

Create a new feature file under the folder

 To create a feature file, we need to right-click on the folder> click on New> go to File>provider File name with ‘.feature’ extension> click on Finish 

Selenium and Cucumber functionality

Install cucumber plugin 

Install the Cucumber plugin from the Eclipse marketplace.

After creating a feature file, we get a message stating there is better editor support for ’*.feature’ files on the marketplace as shown in the image below, so we need to click on Ok to get all the possible plugins that can be used to work with feature files. 

project explorer

>Next, we need to install the Cucumber Eclipse Plugin by clicking on install

Login feature

>accept the terms of the license agreement> click on finish >Restart Eclipse 

After the cucumber plugin installation, we will get modifications done to the feature file having sample texts i.e.., examples on how to create scenarios and steps as shown in the image below: 

Code

Conclusion:

In this blog, we took an overview of what the cucumber java BDD framework is. Learned about the differences between BDD, TDD, and ATDD. Learned how to set up our project in eclipse in order to perform automation tests using selenium and cucumber framework with java, and also went through the file structure for the tests and how the required plugin installation is done.