3

For my program I want to read a key from a properties file and an associated List of values for the key.

Recently I was trying like that

Util:

public class DriverManager {
    public static WebDriver driver;
    String baseUrl;
    public Properties prop;

    public DriverManager() throws IOException {
        // Get baseUrl property
        File file = new File("config.properties");
        FileInputStream fileInput = null;
        try {
            fileInput = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //load properties file
        Map<String,List<String>>categoryMap = new Hashtable<String, List<String>>();


        Properties prop = new Properties();


        try {


            Set<Object> keys = prop.keySet();
            List<String> categoryList = new ArrayList<String>();
            for (Object key : keys) {
                categoryList.add((String)prop.get(key));
                LoginSteps.categoryMap.put((String)key,categoryList);
            }
            System.out.println(categoryList);
            System.out.println("Category Map :"+LoginSteps.categoryMap);

            keys = null;
            prop = null;

        } catch (Throwable e) {
            e.printStackTrace();
        }

property file:

nam=geo,george, sony
pass=1,2,3

I want to do like this, but got some error in my code util package.

1

The path before the test method will be defined by data provider:

@DataProvider
    Object[][] setData() {
        return new Object[][] {
                {"geo", "123"},
                {"geosony", "1"},
                {"geo", "0055"},
                {"", ""}
        };
    }

Rewrite the test method this way:

@Test(dataProvider = "setData")
    public void initDriver(String name, String password) {
        //Arrange
        HomePage page = new LoginSteps().Login(name, password);
}
  • I am keeping my test data in property file. I want to do multiple test data too how ? – user24314 Mar 6 '17 at 4:12
  • Data providers are for TestNG, but jUNit has something similar. The idea is you load the property data in the setData() function. Just loop and push the data into the object. For jUnit look at parameters here: stackoverflow.com/questions/19203730/… – Niels van Reijmersdal Mar 6 '17 at 8:13
  • @NielsvanReijmersdal Hai sir, I edit my post and I am using with array list to read mutilple test data in property file. can you please review ? – user24314 Mar 6 '17 at 8:45
0

Apache PropertyConfiguration support loading of keyValue with same keys.

Below method can return list of values for same key.

  • getList(key) and
  • getStringArray(key)
0

I do not see you loading config.property file to your Property variable.

prop.load(fileInput);

after

Properties prop = new Properties();

should resolve your error. Also, try to keep everything inside one try catch block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy