πŸ“± How to Read Desired Capabilities from Excel and Set Them in DriverFactory – Java + Appium

Dinesh Y SDinesh Y S
2 min read

In mobile test automation using Appium, it’s common to manage desired capabilities directly in the code or through .properties or .json files. But in dynamic enterprise frameworks, reading them from an external Excel file offers better scalability and flexibility.

In this blog, we’ll walk through how to:

βœ… Read desired capabilities from an Excel sheet using Apache POI
βœ… Map them to a DesiredCapabilities object
βœ… Use them in a centralized DriverFactory class

πŸ”§ Tools and Libraries Used

  • Java

  • Appium

  • Apache POI (to handle Excel)

  • TestNG (optional)

  • Excel file (stored in src/test/resources)

πŸ—‚οΈ Excel File Structure

Save your Excel file as Capabilities.xlsx with the following structure:

CapabilityValue
platformNameAndroid
deviceNameemulator-5554
appPackagecom.example.app
appActivitycom.example.Main
automationNameUiAutomator2

πŸ“₯ Step 1: Add Apache POI Dependencies

In your pom.xml:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.4.1</version>
        </dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>

πŸ“„ Step 2: Excel Reader Utility

public class ExcelUtils {

    public static Map<String, String> getCapabilitiesFromExcel(String filePath, String sheetName) {
        Map<String, String> capabilities = new HashMap<>();
        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheet(sheetName);
            boolean firstRow = true;
            for (Row row : sheet) {
                // Skip the first row (header)
                if(firstRow){
                firstRow = false;
                continue;
                }
                Cell keyCell = row.getCell(0);
                Cell valueCell = row.getCell(1);

                if (keyCell != null && valueCell != null) {
                    capabilities.put(keyCell.getStringCellValue(), valueCell.getStringCellValue());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return capabilities;
    }
}

πŸš— Step 3: DriverFactory Class

public class DriverFactory {

    public static AppiumDriver driver;

    public static void initializeDriver() {
        String filePath = "src/test/resources/Capabilities.xlsx";
        Map<String, String> capsMap = ExcelUtils.getCapabilitiesFromExcel(filePath, "Sheet1");

        DesiredCapabilities caps = new DesiredCapabilities();
        for (Map.Entry<String, String> entry : capsMap.entrySet()) {
            caps.setCapability(entry.getKey(), entry.getValue());
        }

        try {
            driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public static AppiumDriver getDriver() {
        return driver;
    }
}

🏁 Conclusion

This approach centralizes capability management and allows QA teams or stakeholders to update device/test configurations without modifying code.

Pros:

  • Easy to update for non-developers

  • Supports multiple test environments

  • Scalable for CI/CD integration

πŸ” In upcoming posts, I’ll explore multi-device testing using data-driven capabilities and CI/CD integrations with Jenkins.

0
Subscribe to my newsletter

Read articles from Dinesh Y S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dinesh Y S
Dinesh Y S

Automation Engineer | Java + Selenium | Appium + Java | RestAssured | Sharing real-world automation tips