top of page
indravadanshrimali94

Cucumber with Java

We are going to learn the simple and easy way to use Cucumber with Java.

Of you search the web you will find many different types of tutorials for it, but sometimes its confusing because every different tutorials have different methods to implement or some of them is outdated or deprecated and not updated.


What is Cucumber?

Cucumber is a testing tool that supports Behavior Driven Development (BDD). It offers a way to write tests that anybody can understand, regardless of their technical knowledge. In BDD, users (business analysts, product owners) first write scenarios or acceptance tests that describe the behavior of the system from the customer’s perspective, for review and sign-off by the product owners before developers write their codes. Cucumber framework uses Ruby Programming Language.




 

How to use Cucumber with java using Selenium?

We can use cucumber combining with different languages and frameworks so we will be going to use it with TestNG Framework using java programming language in Eclipse IDE.

Cucumber can be used to do automation testing to ensure everything is behaving exactly as it supposed to be.


Setting up the project

First, we need to set up the project so we can use Cucumber.

Create a Maven project

In this tutorial, we’re using Maven to import external dependencies. We start by creating a new Maven project, which will automatically create some of the directories and files we will need.

To create a new Maven project in Eclipse:

  1. Click the menu option File > New > Project

  2. In the New project dialog box, select Maven on the left (if it isn’t already selected)

  3. Make sure that the Project SDK is selected (for instance, Java 1.8) and click Next

  4. Specify a GroupId and ArtifactId for your project and click Next

  5. Specify a Project name and Project location for your project (if needed) and click Finish




You will see pom.xml file at the end of the project structure. You can add all the Maven Dependencies we need for Cucumber and TestNG in that file.

To add the dependencies you can simply got to Maven Repositories website and search for dependency.


  • Cucumber Dependency :

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->

<dependency>

<groupId>io.cucumber</groupId>

<artifactId>cucumber-java</artifactId>

<version>7.14.0</version>

</dependency>


<dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-testng</artifactId> <version>7.14.0</version> </dependency>

  • Selenium Dependency

<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.12.1</version> </dependency>

  • TestNG Dependency

<!-- https://mvnrepository.com/artifact/org.testng/testng -->

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>7.8.0</version>

<scope>test</scope>

</dependency>


After adding dependencies, you can just right click on project and Update Maven which will add all files required to the project.


Now follow the below steps :


Step 1:

  1. Create a new folder under src/test/java and give it a name Feature.

  2. Create a file under Feature folder and name it login.feature.

In this login.feature we will write what we are testing. For example we have to test the that user can login in the website or not.


login.feature file :


Feature: Book Cart Application tests Scenario: Login should be success Given User navigate to the Book Cart application And User clicks on the login button And User enter the user name as ortoni And User enter the password as Pass1234 When User click on the login button Then Login should be success

This above file is written in steps and also its called Gherkin Language.

Step 2:


  1. Create a package under src/test/java and name it steps.

  2. Create a java class under the steps package and name it LoginSteps.java.



Step 2:


  1. Create another package under src/test/java and name it testRunner.

  2. Create a java class under testRunner package and name it Runner.java.



Above class is called runnable from where our whole test will run as a TestNG. This is our executable class. The code you can see in @CucumberOptions will connect our login.Feature file and our java classes.




After successfully running the test you will see the output as above.











52 views0 comments

Recent Posts

See All

Comments


bottom of page