Search code examples
javaseleniumselenium-webdriverrest-assuredrelative-url

How to define file path in global.properties file?


I have a global.properties file and have to define the file path inside this properties file.

SheetPath=C:\\Users\\test\\Automation-Scripts\\DataTable.xlsx

This is an absolute path but require a way to define a relative path that can be consumed while calling.


Solution

  • Properties file:

    testPath=API_Files/duplicateToken.json
    

    Load the properties file:

    public static Properties readProperties = new Properties();
    public static void loadPropertiesFile() {
        File propertiesFile = new File(location of properties file);
        try {
            FileInputStream fileInput = new FileInputStream(propertiesFile);
            readProperties.load(fileInput);
        } catch (Exception e) {
            Logger.LogError("Error in loading the Properties file" + e.getMessage());
        }
    }
    

    Read the properties file and get absolute path:

     String testPath = readProperties.getProperty("testPath").trim();
     File absolutePath =  new File(System.getProperty("user.dir") + testPath);
     System.out.println(absolutePath);
    

    Sample output:

    C:\Users\test\Automation-Scripts\duplicateToken.json