Thursday, December 4, 2025

thumbnail

Writing Test Results into Excel Files in Selenium

 Writing Test Results into Excel in Selenium


Selenium itself doesn’t handle Excel files. You need Apache POI, a Java library for reading and writing Excel files (.xls and .xlsx).


1️⃣ Add Apache POI Dependencies


If you use Maven, add this to pom.xml:


<dependencies>

    <!-- Apache POI for Excel -->

    <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>

</dependencies>


2️⃣ Create an Excel File


You can create a blank Excel file manually (TestResults.xlsx) with columns like:


Test Case Status Remarks


Or programmatically using Apache POI:


import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;


public class CreateExcel {

    public static void main(String[] args) throws IOException {

        XSSFWorkbook workbook = new XSSFWorkbook();

        XSSFSheet sheet = workbook.createSheet("TestResults");


        // Create header row

        XSSFRow header = sheet.createRow(0);

        header.createCell(0).setCellValue("Test Case");

        header.createCell(1).setCellValue("Status");

        header.createCell(2).setCellValue("Remarks");


        FileOutputStream fileOut = new FileOutputStream("TestResults.xlsx");

        workbook.write(fileOut);

        fileOut.close();

        workbook.close();


        System.out.println("Excel file created successfully!");

    }

}


3️⃣ Writing Selenium Test Results

Example: Selenium Test + Excel Writing

import org.apache.poi.xssf.usermodel.*;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;


public class SeleniumExcelTest {

    public static void main(String[] args) throws IOException {

        // Set up WebDriver

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();


        // Open Excel

        FileInputStream fis = new FileInputStream("TestResults.xlsx");

        XSSFWorkbook workbook = new XSSFWorkbook(fis);

        XSSFSheet sheet = workbook.getSheet("TestResults");


        int rowCount = sheet.getLastRowNum() + 1;


        try {

            driver.get("https://example.com");

            driver.findElement(By.id("nonExistentId")); // This will throw exception

            // If test passes

            XSSFRow row = sheet.createRow(rowCount++);

            row.createCell(0).setCellValue("Verify Element Presence");

            row.createCell(1).setCellValue("PASS");

            row.createCell(2).setCellValue("Element found successfully");

        } catch (Exception e) {

            XSSFRow row = sheet.createRow(rowCount++);

            row.createCell(0).setCellValue("Verify Element Presence");

            row.createCell(1).setCellValue("FAIL");

            row.createCell(2).setCellValue(e.getMessage());

        } finally {

            FileOutputStream fos = new FileOutputStream("TestResults.xlsx");

            workbook.write(fos);

            fos.close();

            workbook.close();

            driver.quit();

        }


        System.out.println("Test results written to Excel!");

    }

}


4️⃣ Tips for Better Test Result Management


Use a reusable Excel utility class for reading/writing rows.


Use TestNG / JUnit listeners to automatically write results after each test.


Add timestamps or test execution date to track multiple runs.


Handle exceptions carefully to ensure Excel always gets updated.


Style Excel cells using CellStyle to highlight PASS/FAIL results.


5️⃣ Optional: Using Data-Driven Approach


You can also read test data from Excel, run Selenium tests, and write results back in the same sheet — making your automation fully data-driven.

Learn Selenium with JAVA Training in Hyderabad

Read More

Reading Test Data from Excel using Apache POI

๐Ÿ”Œ Data Handling and Advanced Concepts

How to Create a Base Test Class in Java

Reusable Methods in Java for Selenium Tests

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive