Create Object Repository using Properties file

In the previous post, we have seen how to work with WebTables in selenium. In this post, we will learn how to create Object Repository using Properties file.

This object repository will store all the information about the elements available in our application. The data will be stored in the form of key and value pair. This key can be read in the code to fetch the value associated with it. This key can be any kind of locator like XPATH, CSS, id , name etc and the value will its attribute value like XPATH Expression, CSS expression, id value of the element etc.

You can keep it in any package or desired location as per your need. Let’s see how to use it in selenium.

Create a properties file:

Step#1 We have created a package named as ‘com.resources’. Right click on this package and select a new File as shown in the figure below.

Create Properties

 

Step#2 You can give any name to the file and save it with .properties extension.After that click on Finish button.(refer the below figure for understanding). We have given the name ‘OR.properties‘.

Properties file Saving Selenium

 

Step#3 You can edit this file and provide object details with different locators like Xpath, id etc. We have saved the details of objects(elements) present on our demo site. We have stored the first name, last name, radio buttons, links etc. Refer the below figure to see how to add the objects.

Object repository in selenium

Step#4 Create a class file, where we will write the code to use the properties file. Use the below code to understand the usage of properties file in Selenium.

In this code, we are reading the XPATH for ‘First Name’ and ‘Last Name’  from the properties file. Then we are using, those keys to find elements and enter the data into them.

public class PropExample {
	private static WebDriver driver;
	public static void main(String[] args) throws IOException {
		
		// Create the file instance using the path where file stored
		FileInputStream file = new FileInputStream(System.getProperty("user.dir")+"\\src\\com\\resources\\OR.properties");
		Properties prop = new Properties();
		prop.load(file);        //Load the properties file
		
		//Print the value of xpath stored in OR.properties 
		System.out.println(prop.getProperty("FirstName_xpath"));
		System.out.println(prop.getProperty("Last_Name_xpath"));
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		
		//enter the first name and last name 
		// pick the object property from OR.properties file
		
		driver.findElement(By.xpath(prop.getProperty("FirstName_xpath"))).sendKeys("Shekhar");
		driver.findElement(By.xpath(prop.getProperty("Last_Name_xpath"))).sendKeys("Sharma");
	}

}

Output:

//*[@id=’firstname’] //*[@id=’lastname’]

Read properties file in selenium


Advantage of using Properties file as object repository:

If we use properties file, then all the objects will be at common place. Suppose, If there is any change in any of the object property like xpath, id etc. and if that xpath has been hard coded in multiple test cases. Then ,we have to update each and every test case class file.

But if xpath is placed in properties file, then we have to update the changed xpath value only in at one place i.e. properties file. This properties file will be associated as object repository with all multiple test cases.


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

1 Response

  1. Shravan says:

    If property file changes is it require Server Restart.
    Normally it requires Server Restart.

    Please confirm if i uses these one so i need to restart after passing values dynamically.