top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Reading data from Excel sheet in Cucumber

Introduction:


In this blog post, we will explore how to read data from an Excel sheet within a BDD(Behavior-driven development)Cucumber framework. One common requirement in test automation is reading data from external sources, such as Excel sheets. We implemented this in our “DSAlgo-Project”.


Step1: Set up the project


1. Create a new Maven project.

2. Add necessary dependencies to our project’s build file.

· Cucumber-Java: Enables Cucumber support in java.

· Cucumber-Junit: Integrates Cucumber with Junit for test execution.

· Apache POI: Provides support for reading Excel files.

3. Set up the project directory structure with feature files, step definitions, and utility classes.


Step2: Create the Feature File

1. We Created a new feature file with a descriptive name, such as “_signin.feature”.

2. We Define our feature and scenarios using Gherkin syntax.

For example:

Excel Sheet that we used for Signin:

We have created one folder called TestData in that we can see Excelfile.


Step3: Implement Step Definitions

1. Create a step definition class corresponding to the feature file.

2. Define step definitions for each step in the feature file.

In your Cucumber step definitions, you can now use the Excel reader under Utilities package to read data from the Excel sheet.

For example:

Step4: SignIn_Page Class:



POM File:

To read data from an Excel sheet, we need to add an external library that provides support for interacting with Excel files. To add Apache POI to your project, include the following dependency in your build file.


<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>5.2.3</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>5.2.3</version>

</dependency>

Excel reader:

We need to create a Excel Reader class under Utilities package that encapsulates the logic for reading data from the Excel sheet. This class will interact with the Apache POI library to extract the necessary data. Here's an example of a Excel Reader class that reads data from an Excel sheet:


This Excel reader class 'Workbook' object using 'WorkbookFactory' from the Apache POI library. It retrieves the desired sheet and iterates over the rows and cells to extract the data.


Step5: Run the cucumber Test

  1. Create a runner class or use the existing one to configure the test execution.

  2. Run the Cucumber test to execute the feature file and step definitions.

Conclusion:


In this blog post, we explored how to read data from an Excel sheet within BDD Cucumber framework. By using Apache POI libraries and implementing the necessary step definitions we can extract data from Excel sheet and use it in our BDD scenarios.

Thank you for reading and give your valuable comments to improvise.


3,692 views4 comments

Recent Posts

See All

Beginner Friendly Java String Interview Questions

Hello Everyone! Welcome to the second section of the Java Strings blog. Here are some interesting coding questions that has been solved with different methods and approaches. “Better late than never!”

4 commenti

Valutazione 0 stelle su 5.
Non ci sono ancora valutazioni

Aggiungi una valutazione
Ospite
02 ott 2023

That is a great solution indeed, Question: how can I iterate through the rows in the excel file dynamically? I'd like to have a feature that consumes entire excel file and executes the feature against ALL the rows that are in there. Got any hints?

Mi piace
Sravanthi Balla
Sravanthi Balla
17 ott 2023
Risposta a

Plesae refer the ExcelReader class file there you can find totalrow=sheet.getLastRowNum

this will take the last row from your excel sheet and then 'for loop' is used to get all rows one after the other.

Mi piace

Ospite
17 ago 2023
Valutazione 5 stelle su 5.

Well explained.

Mi piace
Sravanthi Balla
Sravanthi Balla
18 ago 2023
Risposta a

Thank you


Mi piace
bottom of page