.properties is a file extension for files mainly used in Java to store the configurable parameters of an application.
the parameters are in terms of key | value pair, means we can use the key in the code to get the value stored in the .properties file.
This basically helps to avoid going to the code for any change needed in the value, we can directly change in the file itself.
Open a notepad, enter key and values and “Save As” – “config.properties”
Usage:
Normally in framework, we use .properties file to store Username, Password, URL under test, browser type / version, platform etc…it helps each automation tester to use their own details in program and also helps to keep their credentials confidential. Let’s understand more about the .properties file!
Format:
- [key] = [value] or [key] : [value]
- For comments, statements starts with either ! or #.
- White space at beginning or before and after of = or : are ignored.
- Blank lines are ignored.
- If value is written in multi line, then each line should be ended with ” (backslash).
Let’s see the code to read the values(MethodName-getVal()) from the key/variable we entered in the .properties file and how to update/write values to the keys(MethodName-setVal()).
Let’s consider the config.properties
file as below
browser=chrome fullName=qavbox telephone=1234567890 excelPath=/Users/MyUser/testData.xls url=https\://qavbox.github.io/demo/signup/ email=qavbox@gmail.com username=JohnConer
Let’s see the code implementation to read & write the data
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; public class properties { public static String getVal(String key) { String keyval = null; //Let's consider properties file is in project folder itself File file = new File("./config.properties"); //Creating properties object Properties prop = new Properties(); //Creating InputStream object to read data FileInputStream objInput = null; try{ objInput = new FileInputStream(file); //Reading properties key/values in file prop.load(objInput); keyval = prop.getProperty(key); objInput.close(); }catch(Exception e){System.out.println(e.getMessage());} return keyval; } public static void setVal(String key, String keyval) { File file = new File("./config.properties"); Properties prop = new Properties(); FileInputStream objInput = null; try { objInput = new FileInputStream(file); prop.load(objInput); objInput.close(); FileOutputStream out = new FileOutputStream("./config.properties"); prop.setProperty(key, keyval); prop.store(out, null); out.close(); } catch (Exception e) {System.out.println(e.getMessage());} } public static void main(String[] args) { System.out.println("Browser: " + getVal("browser")); System.out.println("URL: " + getVal("url")); System.out.println("fullName: " + getVal("fullName")); System.out.println("Excel Path: " + getVal("excelPath")); setVal("username", "JohnConer"); System.out.println("After updating properties file"); System.out.println("username: " + getVal("username")); } }
Out Put:
Browser: chrome URL: https://qavbox.github.io/demo/signup/ fullName: qavbox Excel Path: /Users/MyUser/testData.xls After updating properties file username: JohnConer
Explanation –
Created prop object from Properties class.
Loading the .properties file using prop.load()
method.
Then by using prop.getProperty(key)
, we will fetch the value from the file.
The same way, we can use prop.setProperty(Key, Value)
to write data to .properties file.
In this above example, I have explained using main method, but you can call the getValue(String key)
to get the value from anywhere from your framework.
Recommended – You can create an utility class & capture all the data from the file and put into a Map, it’s easy to fetch the data from Map to any test or page object classes. so you need not to load the properties file every time you fetch the data.
If this is helpful, then you might be interested to refer How to read / use excel sheet as test data in selenium.