Parameterize properties file in Java

10 March, 2015
If you are using a properties file and passing key value pairs, there are instances when you would want to parameterize the values passed. For instance the dynamic xpaths need to be parameteized for flexibility.

Steps to achieve this :

Your properties file should have the key value as :
LoginPage.DynamicObject.Button=//*[@text=''{0}'' and @width>0]
In the above statement {0} is the parameter
In your class file that calls the property
Properties obprop = new Properties();
String Locator= MessageFormat.format(obprop.getProperty(Object_Identifier), params);
where Object_Identifier is HomePage.DynamicObject.Button
and params is the parameter you want to pass

Note: Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. .

Any unmatched quote is treated as closed at the end of the given pattern. For example, pattern string "'{0}" is treated as pattern "'{0}'".

No comments:

Post a Comment