Properties File in Selenium | Properties prop = new Properties()

Properties File in Selenium :

Properties file is used to store project configuration data or settings in the the key value pair format.

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application.

Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key/map), and the other storing the value.

 

Properties Class in java:

properties Class can be used to get property value based on the property key. The Properties class provides methods to get data from properties file and store data into properties file.

Read  Properties file using Java Selenium:

For reading the file we have to create a file with .properties and store some data in it.

url = htttp://google.com
searchTerm = chercher tech

 

Steps to read properties file in selenium:

  1. Create object to FileInputStream with file path
  2. Create object for Properties class
  3. Load the    FileInputStream Object to the load method of properties class
  4. Using getProperty() method read the required(url) field from the properties file
  5. use the retrieved value according to your requirement
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("searchTerm"));
	}
}

 

Write data to properties file in selenium :

Sometime you may need to write the data into the properties file from the browser or from the other files.

Let’s see how to write a properties file in selenium java.

  1. Create Properties class object
  2. Using setProperty() method set the key value pair to the properties file
  3. Now push all the properties file content into FileOutputStream object
  4. write the FileOutputStream  to the local system
  5. If any error happen at above then control goes to the catch block.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class WritePropertiesFile {
	public static void main(String[] args) {
		try {
			Properties properties = new Properties();
			properties.setProperty("website", "javaSeleniumWorld");
			properties.setProperty("author", "pk");
			properties.setProperty("designation", "software engg");
			FileOutputStream fOut = new FileOutputStream(file);
			properties.store(fOut, "Website details");
			fOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Few Things about properties files:

  • Entries are generally expected to be a single line of the form
  • White space that appears between the property name and property value is ignored
  • White space at the beginning of the line is also ignored.
  • Lines that start with the comment characters ! or # are ignored. Blank lines are also ignored.
  • The property value is generally terminated by the end of the line. White space following the property value is not ignored, and is treated as part of the property value.
  • A property value can span several lines if each line is terminated by a backslash (‘\’) character.
  • The backslash character must be escaped as a double backslash
  • UNICODE characters can be entered as they are in a Java program, using the \u prefix

 

Leave a Reply

Your email address will not be published. Required fields are marked *