Properties files stores simple parameters as key-value pairs, outside of compiled code. Key and Values are separated using '=' sign
Most of the time properties file used to store url, username, password, environment etc..
Eg:
Rules about properties files
Output :
propertyName=propertyValue
propertyName:propertyValue
Output :
name=cherchertech
name = cherchertech
White space at the beginning of the line is also ignored.
Output :
targetCities=\
Detroit,\
Chicago,\
Los Angeles
This is equivalent to targetCities=Detroit,Chicago,Los Angeles (white space at the beginning of lines is ignored).
path=c:\\docs\\doc1We can read and write the properties file in selenium using the Properties class present in the Java, this class provide few methods to read and write the data into properties file.
Insights about properties files
Follow below steps to read property file
1. Create a Java class on Eclipse
2. Create a properties file on your local machine with .properties extension.
3. Create Object for FileInputStream with .properties file path, it makes the file into stream of input item
// create file input stream object for the properties file
FileInputStream fis = new FileInputStream("C:\\path\\config.properties");
4. Create Properties class object to access the property file.
// create object for Properties class
Properties prop = new Properties();
5. Load the property file using load() present in the Properties object
// Load properties file
prop.load(fis);
6. Get the values using 'get("key")' method or getProperty("key") method by passing key as the patrameter
prop.getProperty("author");
prop.get("user");
Complete Program to Read value from properties file with java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadProperties {
public static void main(String[] args) throws IOException {
// create file input stream object for the properties file
FileInputStream fis = new FileInputStream("C:\\path\\config.properties");
// create Properties class object to access properties file
Properties prop = new Properties();
// load the properties file
prop.load(fis);
// get the property of "url" using getProperty()
System.out.println(prop.getProperty("url"));
System.out.println(prop.getProperty("author"));
// get the property of "url" using get()
System.out.println(prop.get("user"));
}
}
Output :
https://chercher.tech
unknown
any one in the world
To read files from the class path, we have to give the relative path to the file. We can retrieve the class path using
All the steps will remain same except the path of the file
// create file input stream object for the properties file
FileInputStream fis = new FileInputStream(this.getClass().getResourceAsStream("/test.properties"));
// create Properties class object to access properties file
Properties prop = new Properties();
Sometimes properties could be in different format like '.txt' or other extension, file could have any extension still we can use the same methods what we used in .properties file.
config file
This program shows how to read Properties files in text format
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTextExtension {
public static void main(String[] args) throws IOException {
// create file input stream object for the properties file
FileInputStream fis = new FileInputStream("C:\\path\\config-p.txt");
// create Properties class object to access properties file
Properties prop = new Properties();
// load the properties file
prop.load(fis);
// get the property of "url" using getProperty()
System.out.println(prop.getProperty("time"));
// get the property of "url" using get()
System.out.println(prop.get("hour"));
}
}
Output :
Follow below steps to write property file
1. Create a Java class on Eclipse
2. Create a properties file on your local machine with .properties extension(config-write.properties).
3. Create Object for FileOutputStream with .properties file path, it makes the file into stream of output item
// create file output stream object for the properties file
FileOutputStream fos = new FileOutputStream("C:\\path\\config-write.properties");
4. Create Properties class object to access the property file.
// create object for Properties class
Properties prop = new Properties();
5. Set the values using getProperty("key","Value") method by passing key and values as the parameters
// set the properties
prop.setProperty("Selenium", "https://chercher.tech");
6. Store the values onto file commit it to local file system using prop.store(fos)
// set the properties
prop.setProperty("Selenium", "https://chercher.tech");
7. It will store the values along with the committed time on the first line
Complete Program to Write values to properties file with java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class WriteProperties {
public static void main(String[] args) throws IOException {
// create file output stream object for the properties file
FileOutputStream fis = new FileOutputStream("C:\\path\\config-write.properties");
// create Properties class object to access properties file
Properties prop = new Properties();
// load the properties file
// set the properties
prop.setProperty("Selenium", "https://chercher.tech");
prop.setProperty("Google", "https://google.com");
prop.setProperty("Yahoo", "https://yahoo.com");
// store the file into local system
prop.store(fis, null);
}
}
When we are reading a property file we can look into the .properties n-number of times but sometimes we may need to read the all the properties to verify something either on application or on the properties file.
config file
This program shows how to Read All Values in properties file
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;
public class ReadAllProperties{
public static void main(String[] args) throws Exception{
// create file input stream object, to make file as readable by machine
FileInputStream fis = new FileInputStream("C:\\path\\config-read-all.properties");
// create properties class object to access all non-static methods
Properties properties = new Properties();
// load the required .properties file
properties.load(fis);
// close the file as Properties class object have all the details
fis.close();
// Properties work like HashTable, so we have to handle like hash table only
Enumeration enumKeys = properties.keys();
// iterate till the enumKeys has keys
while (enumKeys.hasMoreElements()) {
// move from null to first element(key), by default it will not point to first element
String key = (String) enumKeys.nextElement();
// fetch the property for the key
String value = properties.getProperty(key);
System.out.println(key + " = " + value);
}
}
}
This methods not only for selenium but also you can use where ever you use java to read properties files.
There could be a scenarios where you have read values from properties file but you might have stored them in different files.
For example : login.properties, config.txt and so on, please Read Properties file in other formats if you need more information why we are using '.txt' file
Files
prop.load(fis);
prop.load(fis2);
Complete program to read Multiple properties files
// PLEASE DO WRITE IMPORT STATEMENTS
public class ReadMultiplePropertiesFiles {
public static void main(String[] args) throws IOException {
// create file input stream object for the properties file
FileInputStream fis = new FileInputStream("C:\\path\\login.properties");
FileInputStream fis2 = new FileInputStream("C:\\path\\config.txt");
// create Properties class object to access properties file
Properties prop = new Properties();
// load the properties file
prop.load(fis);
//load all the files
prop.load(fis2);
// get the property of "url" using getProperty()
System.out.println(prop.getProperty("url"));
System.out.println(prop.get("suite"));
}
}
When we are reading a property file we can look into the .properties n-number of time but it takes lot of memory and process to read the value each time from the file.
To avoid this in our frameworks we have to keep all the properties in a map, so that we can retrieve the values from this map when it is required
This program shows how to Convert properties file values to Java Map
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class PropertiesToJavaMap {
public static void main(String[] args) throws Exception {
// declare a map which store string as key and string as value
Map mapProp = new HashMap();
// create file input stream object, to make file as readable by machine
FileInputStream fis = new FileInputStream("C:\\path\\read-all.properties");
// create properties class object to access all non-static methods
Properties prop = new Properties();
// load the required .properties file
prop.load(fis);
// close the file as Properties class object have all the details
fis.close();
// Properties work like HashTable, so we have to handle like hash table only
Enumeration enumKeys = prop.keys();
// iterate till the enumKeys has keys
while (enumKeys.hasMoreElements()) {
// move from null to first element(key), by default it will not point to first element
String key = (String) enumKeys.nextElement();
// fetch the property for the key
String value = prop.getProperty(key);
// store the key and value in map
mapProp.put(key, value);
}
System.out.println("Value stored to Map are : "+mapProp);
}
}
Output :
Sometimes we may in a situation where we have to store the java map to properties file, end of the we have to store the value for retrieving in future.
config file
This program shows how to Convert Java Map to properties file
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class JavaMapToProperties {
public static void main(String[] args) throws IOException {
// declare a map which store string as key and string as value
Map mapProp = new HashMap();
mapProp.put("Name", "unknow");
mapProp.put("Age", "27");
mapProp.put("Gende", "Male");
File file = new File("C:\\path\\map-to-file.properties");
// create file input stream object for the properties file
FileOutputStream fos = new FileOutputStream(file);
// create Properties class object to access properties file
Properties prop = new Properties();
// load the properties file
Iterator keys = mapProp.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
prop.setProperty(key, mapProp.get(key));
}
prop.store(fos, null);
}
}
Output :
Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know.
Knowledge is the only thing that doubles when you spend it.
I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation