Paperwork

5 Ways to Compare Excel Sheets in Selenium WebDriver Java

5 Ways to Compare Excel Sheets in Selenium WebDriver Java
How To Compare Two Excel Sheets In Selenium Webdriver Java

If you've found yourself needing to compare Excel spreadsheets, whether it's for reconciling financial data, checking data consistency across different systems, or performing automated quality checks, Selenium WebDriver with Java offers a powerful solution. Here, we delve into 5 different methods to compare Excel sheets using Selenium WebDriver with Java. These methods can streamline your automation tasks, ensuring accuracy and efficiency in data handling.

Method 1: Apache POI with Direct Cell Comparison

Selenium Webdriver Getting Data From Excel Stack Overflow

Apache POI is a robust library for reading and writing Excel files in Java. Here, we'll use POI to compare two sheets cell by cell:


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

public class ExcelComparator {
    public void compareSheets(Workbook workbook1, Workbook workbook2) {
        Sheet sheet1 = workbook1.getSheetAt(0);
        Sheet sheet2 = workbook2.getSheetAt(0);

        for (int rowIndex = 0; rowIndex < sheet1.getLastRowNum(); rowIndex++) {
            Row row1 = sheet1.getRow(rowIndex);
            Row row2 = sheet2.getRow(rowIndex);

            for (int cellIndex = 0; cellIndex < row1.getLastCellNum(); cellIndex++) {
                Cell cell1 = row1.getCell(cellIndex);
                Cell cell2 = row2.getCell(cellIndex);

                if (cell1 == null || cell2 == null) {
                    System.out.println("Mismatch at Cell [" + rowIndex + "][" + cellIndex + "]");
                } else if (!cell1.toString().equals(cell2.toString())) {
                    System.out.println("Mismatch at Cell [" + rowIndex + "][" + cellIndex + "]");
                }
            }
        }
    }
}

ℹī¸ Note: Apache POI offers versatile Excel manipulation, but this method can be resource intensive for large datasets.

Method 2: Compare Specific Ranges

Finding Matching Data In Two Excel Sheets

Sometimes, you don't need to compare entire sheets but specific ranges or cells:


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

public class RangeComparator {
    public void compareRange(Workbook wb1, Workbook wb2, int startRow, int startCol, int endRow, int endCol) {
        Sheet sheet1 = wb1.getSheetAt(0);
        Sheet sheet2 = wb2.getSheetAt(0);

        for (int row = startRow; row <= endRow; row++) {
            Row r1 = sheet1.getRow(row);
            Row r2 = sheet2.getRow(row);

            for (int col = startCol; col <= endCol; col++) {
                Cell c1 = r1.getCell(col);
                Cell c2 = r2.getCell(col);

                if (c1 == null || c2 == null || !c1.toString().equals(c2.toString())) {
                    System.out.println("Mismatch at Cell [" + row + "][" + col + "]");
                }
            }
        }
    }
}

Method 3: Using Apache POI for Sheet Comparison

Read Data From Excel File In Selenium Webdriver L Y D Li U T Excel

Instead of comparing cell by cell, Apache POI can compare sheets directly:


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

public class SheetComparator {
    public void compareSheets(Workbook wb1, Workbook wb2) {
        Sheet sheet1 = wb1.getSheetAt(0);
        Sheet sheet2 = wb2.getSheetAt(0);

        for (int i = 0; i < sheet1.getPhysicalNumberOfRows(); i++) {
            Row row1 = sheet1.getRow(i);
            Row row2 = sheet2.getRow(i);

            if (!row1.toString().equals(row2.toString())) {
                System.out.println("Mismatch at Row " + i);
            }
        }
    }
}

🔍 Note: This method compares sheets row by row, which can be faster for large sheets but might miss cell-specific discrepancies if rows are identical except for order.

Method 4: Using ExcelDiff for Visual Comparison

Handling Dynamic Web Tables Using Selenium Webdriver

ExcelDiff is a tool that provides visual comparison of Excel spreadsheets:

  • Download ExcelDiff from the official source.
  • Integrate ExcelDiff into your Java project.
  • Use the API to compare sheets:

import exceldiff.ExcelDiff;

public class VisualComparison {
    public void compareSheets(String file1, String file2) {
        try {
            ExcelDiff.main(new String[]{"-f", file1, file2, "-o", "comparison_result.xlsx"});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Method 5: JUnit Based Comparison

Open Multiple Tabs In Chrome Selenium Automation Excel Vba Code

For testing purposes, using a testing framework like JUnit can be beneficial:


import org.junit.Test;
import static org.junit.Assert.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelTest {
    @Test
    public void testExcelSheets() throws Exception {
        Workbook wb1 = WorkbookFactory.create(new File("workbook1.xlsx"));
        Workbook wb2 = WorkbookFactory.create(new File("workbook2.xlsx"));

        Sheet sheet1 = wb1.getSheetAt(0);
        Sheet sheet2 = wb2.getSheetAt(0);

        // Compare cells
        for (int i = 0; i < sheet1.getPhysicalNumberOfRows(); i++) {
            Row row1 = sheet1.getRow(i);
            Row row2 = sheet2.getRow(i);

            if (row1 == null || row2 == null) {
                fail("Mismatch at row " + i);
            }

            for (int j = 0; j < row1.getPhysicalNumberOfCells(); j++) {
                Cell cell1 = row1.getCell(j);
                Cell cell2 = row2.getCell(j);

                if (cell1 == null || cell2 == null) {
                    fail("Mismatch at cell [" + i + "][" + j + "]");
                }

                assertEquals(cell1.toString(), cell2.toString());
            }
        }
    }
}

To conclude, comparing Excel sheets in Selenium WebDriver Java provides various approaches tailored to different needs, from direct cell comparison to visual analysis. These methods not only enhance your automation capabilities but also ensure data accuracy and consistency across various applications. By mastering these techniques, you can automate complex data validation tasks effectively.

Why use Selenium WebDriver for Excel sheet comparison?

How To Compare Two Excel Sheets And Highlight Differences Prodev
+

Selenium WebDriver, while primarily used for web automation, can be combined with libraries like Apache POI to handle Excel files. This combination provides a powerful toolset for automated testing and data comparison tasks, especially in scenarios where data validation across systems is required.

Can these methods detect formatting differences?

How To Verify The Title Of The Page And Compare With Expected Value
+

The methods outlined above mainly focus on the content within cells. To detect formatting differences, you would need to look into libraries that support cell styling comparisons or use visual comparison tools like ExcelDiff.

Which method is best for large Excel sheets?

Compare Two Excel Worksheets
+

For large Excel sheets, using Apache POI for sheet comparison or ExcelDiff might be more efficient as they can process sheets faster than cell-by-cell comparisons. However, for precise data validation, cell-by-cell comparison or specific range comparisons are recommended.

Is there a way to compare Excel sheets without Java?

A K Sahu S Blog Data Driven Testing In Selenium Webdriver With
+

Yes, there are several tools and online services available for comparing Excel sheets without programming knowledge. Tools like Microsoft Excel itself, with its in-built compare feature, or online tools like DiffEngineX can be used for quick comparisons.

Related Articles

Back to top button