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.


5,761 views5 comments

5 comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
Convidado:
29 de mai.
Avaliado com 1 de 5 estrelas.

It is not complete, it marks me error in Excelpath and it does not show me the option to import class

Curtir

Convidado:
02 de out. de 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?

Curtir
Sravanthi Balla
Sravanthi Balla
17 de out. de 2023
Respondendo 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.

Curtir

Convidado:
17 de ago. de 2023
Avaliado com 5 de 5 estrelas.

Well explained.

Curtir
Sravanthi Balla
Sravanthi Balla
18 de ago. de 2023
Respondendo a

Thank you


Curtir
bottom of page