Creating configuration properties for Selenium Test Suite

by seetaram on June 20, 2011

Create a configuration properties file to store parameters such as log file path, test data path, results path, selenium execution speed, wait period, etc, then make changes to the parameters whenever needed.

Java provides a way to create configuration file for storing configurable parameters of an application.  Selenium Test Suite also needs such a file to keep global configuration settings.

In this post let us try to understand how we can create a “.properties” file and how to access values from the properties file.

Create a file text file (using any text editor such as notepad) Selconfig.properties (as in the file – view file) and save the file as “Selfconfig.properties

Create classes as in the following code snippets:

package com.selftechy.readdata;

/*
 * Author - Seetaram Hegde
 * 
 */

import com.selftechy.library.*;
import java.io.IOException;

public class TestRun {
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		config cfg = new config("F:\\Helios-Workspace\\Config\\SelConfig.properties");
		System.out.println(cfg.ReadProperty("ConfigPath"));

	}

}

 

package com.selftechy.library;

/*
 * Author - Seetaram Hegde
 * 
 */

import java.io.*;
import java.util.*;


public class config {
	  String str, key;
	  private String filepath;
	  
	  public config(String filepath){
		  this.filepath=filepath;
	  }
	  
	  public String ReadProperty(String propkey){
		  String propval="";
		  try{
			  int check = 0;
			  while(check == 0){
				  check = 1;
				  File cfgfile = new File(filepath);
				  if(cfgfile.exists()){
						  Properties props = new Properties();
						  FileInputStream propin = new FileInputStream(cfgfile);
						  props.load(propin);
						  propval=props.getProperty(propkey);
					  }
					  else{
						  check = 0;
				  }
				}
			  }
			  catch(IOException e){
				  e.printStackTrace();
			  }
		return propval;	  
	  }	
}

 

Save both the classes and then execute the first one (TestRun.java) as Java application.  In the Selconfig.properties file, TestData & ConfigPath are the keys and “F:\\Helios-Workspace\\TestData & F:\\Helios-Workspace\\Config” are the respective values.  In .properties file we can put comments using “#”.  Any sentence that starts with # will be considered as comment.

To access the values of any properties, use the “key” parameter.  For example, to access the value of “TestData” and “ConfigPath” write the code as follows:

config cfg = new config("F:\\Helios-Workspace\\Config\\SelConfig.properties");


cfg.ReadProperty("ConfigPath")

cfg.ReadProperty("TestData")

{ 3 comments… read them below or add one }

sandeep June 13, 2012 at 7:46 am

Appreciate your knowledge sharing. Not many people do this!

Reply

Nusha September 11, 2012 at 11:03 am

Hello! I copied and pasted the above codes in Eclipse. I created the .properties file and saved it on my desktop.

I am experiencing the following error when i run the TestRun.java :

Exception in thread “main” java.lang.NullPointerException
at java.io.File.(Unknown Source)
at com.selftechy.library.config.ReadProperty(config.java:28)
at com.selftechy.readdata.TestRun.main(TestRun.java:14)

Can you help me to sort out this issue?

Reply

karan November 15, 2013 at 4:54 pm

Hi Nusha,

I know its been long since you posted this, but did you found out a solution for the error?

Thanks

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email