0
0
Selenium Javatesting~10 mins

Excel data reading (Apache POI) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test reads data from an Excel file using Apache POI and verifies that the expected value is present in a specific cell.

Test Code - JUnit
Selenium Java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.*;
import java.io.FileInputStream;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;

public class ExcelDataReadingTest {

    @Test
    public void testReadExcelCell() throws IOException {
        String filePath = "src/test/resources/testdata.xlsx";
        FileInputStream fis = new FileInputStream(filePath);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(1); // second row (index 1)
        Cell cell = row.getCell(0); // first column (index 0)
        String cellValue = cell.getStringCellValue();
        workbook.close();
        fis.close();

        assertEquals("TestUser", cellValue, "Excel cell value should be 'TestUser'");
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initialized-PASS
2Opens Excel file 'src/test/resources/testdata.xlsx' using FileInputStreamExcel file stream opened successfully-PASS
3Creates XSSFWorkbook object from file streamWorkbook loaded with sheets and data-PASS
4Accesses first sheet (index 0)Sheet object representing first sheet ready-PASS
5Reads second row (index 1) and first cell (index 0)Cell object with string value retrieved-PASS
6Extracts string value from cellString value 'TestUser' obtained-PASS
7Closes workbook and file input streamResources released-PASS
8Asserts that cell value equals 'TestUser'Assertion compares expected and actual valuesassertEquals("TestUser", cellValue)PASS
Failure Scenario
Failing Condition: Excel file missing or cell value different than expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after reading the Excel cell?
AThat the cell is empty
BThat the cell value is 'TestUser'
CThat the Excel file has at least 3 sheets
DThat the workbook is saved
Key Result
Always close the workbook and file input stream to free resources and avoid file locks after reading Excel data.