Monday

Parameterize properties file in Java



If you are using a properties file and passing key value pairs, there are instances when you would want to parameterize the values passed. In my case the dynamic xpaths need to be parameteized for more flexibility

Solution: MessageFormat

Steps to achieve this :


  1. Your properties file should have the key value as :
    1. HomePage.DynamicObject.Button=//*[@text=''{0}'' and @width>0] 
  2. In the above statement {0} is the parameter 
  3. In your class file that calls the property
    1. Properties obprop = new Properties();
    2. String Locator= MessageFormat.format(obprop.getProperty(Object_Identifier), params);
    3. where Object_Identifier is HomePage.DynamicObject.Button
    4. 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