Update Property Values During Runtime in Java
Share
For every project Properties file play major factor. In this article we would look into how to update property values during runtime in java.
We provide constant values required through property files. Properties are static key value pairs read during start of project.
In a case if I want to use a value from current execution in next run?.
For an instance, I want to compare failed test case count from last run with current execution in my automation project.
I can achieve this by storing value in a test results file and read it back in next run. Another way is to store a value in my property file with key “LastFailedcount” and value from current execution.
Storing key value pair is easier for me as I don’t need to look for last results file every time. And property file initiation is one time activity for each execution.
Updating a property value during runtime in java, how?.
First let us recall how to read a properties file,
Reading Properties file
package automation; import java.io.InputStream; import java.util.Properties; public class rwProperties{ //Initiating Java Properties class object public static Properties prop = new Properties(); public void readProperties() { try { //Reading the fiel as Inputstream from Project Resources path // File located in Properties folder under Project Resources InputStream input = Main.class.getResourceAsStream("/properties/prop.xml"); // This would load the inputstream to properties class of java prop.loadFromXML(input); //Close the inputstream to reduce resource leak input.close(); } catch (IOException | NullPointerException e) { System.out.print(e) } } }
now you will use the desired key value by
prop.getProperty(<key); //here our key is "LastFailedCount" //then it looks like prop.getProperty("LastFailedCount");
Let us see how can we update property values during runtime. I took example of Properties file in XML format in this tutorial.
For an instance I got my Property file with Key “LastFailedCount”, which records failed testcase count from last execution.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="LastFailedCount">10</entry> </properties>
I want this count to be used to compare and provide failure percentage metrics for last vs current execution.
Once I generate metric I want to replace the failure count from current execution. So next time this value can be used.
First create a object for Java Properties class, below creates a object that has no default values.
Properties newPropValue = new Properties();
Now load already read properties to this object.
newPropValue.putAll(prop);
Then update the desired key value as required
newPropValue.setProperty("LastFailedCount", newValue); //here newValue would be the Failed count you recorded as par tof your current run.
You need to then read the file to which you want to update. If you want to use same way how you read the file in readProperties();
then it would not work out while saving the file back.
For this you need to get the absolute path for the file.
//System.getProperty("user.dir") will return the eaxct location of project directory on the system File file = new File(System.getProperty("user.dir") + "/src/test/resources/properties/prop.xml");
Then you need to load the file to output stream using FileOutputStream class of java.io
FileOutputStream out = new FileOutputStream(file);
Then write / store the value to the file.
// Syntax for storing is (File, Comment for the update) //Comment is optional //Here I am using local time when it updated. newPropValue.storeToXML(out, "Count Updated at "+ LocalDate.now());
Finally close the output stream, this would help to reduce resource leak.
out.close();
Complete method looks as,
public void writetoProp(String key, String propValue) { Properties newPropValue= new Properties(); newPropValue.putAll(prop); newPropValue.setProperty(key, propValue); FileOutputStream out; try { file = new File(System.getProperty("user.dir") + "/src/test/resources/properties/prop.xml"); out = new FileOutputStream(file); newPropValue.storeToXML(out, "ID Updated on "+ LocalDate.now()); out.close(); } catch (IOException e) { Sytem.out.print(e) } }
This is how you can update property value during runtime.
Let us look at whole class with read and write to property file.
package automation; import java.io.InputStream; import java.util.Properties; import java.io.IOException; import java.io.FileOutputStream; import java.time.LocalDate; public class rwProperties{ //Initiating Java Properties class object public static Properties prop = new Properties(); public void readProperties() { try { //Reading the fiel as Inputstream from Project Resources path // File located in Properties folder under Project Resources InputStream input = Main.class.getResourceAsStream("/properties/prop.xml"); // This would load the inputstream to properties class of java prop.loadFromXML(input); //Close the inputstream to reduce resource leak input.close(); } catch (IOException | NullPointerException e) { System.out.print(e) } } public void writetoProp(String key, String newValue) { Properties newPropValue= new Properties(); newPropValue.putAll(prop); newPropValue.setProperty(key, newValue); FileOutputStream out; try { file = new File(System.getProperty("user.dir") + "/src/test/resources/properties/prop.xml"); out = new FileOutputStream(file); newPropValue.storeToXML(out, "ID Updated on "+ LocalDate.now()); out.close(); } catch (IOException e) { Sytem.out.print(e) } }
You need to call this updating method finally when your site execution finish.
@AfterSuite public void updaetFailedCount(){ //Failed count is the variable which stores value to be updated writetoProp("LastFailedCount", failedCount) }
You may like to look into OOPS concepts in automation.