Home > Automation Testing > How to use properties file and xml file as object repository in selenium

How to use properties file and xml file as object repository in selenium

This post was most recently updated on August 5th, 2024

Code to read data from properties and xml file in Selenium for Object Repository.

How to store and read data from properties file

what is properties file ?

Properties file is a text file with .properties as extension where tester can store environment configuration details  like browser name,driver path ,url,userid , password that are common for all test scripts of a application.We can also use properties file as Object Repository and store path of different Web elements.

Properties file store the data in key-value approach where key refer to object and value refer to property of that object.

Let us assume that tester created different test classes for a application and tester is using chrome browser.So tester has to pass the path of chrome driver in each test class.After sometime if he  change the path of chrome driver he has to update the path in all the test classes of the application which is not a good approach.So what he can do is he will store the path of chrome driver in properties file and read the data from that file.If he wants some modification he can do it in the properties file and data will updated in all test classes.

To use  properties file as Object Repository and to read data from properties file follow the below steps.

You can find code at end here is explanation of that code.

1. Create a file with .properties  as extension
2. Store value in key value format
3.Write code to fetch the data from properties file.
To read the data from properties file write the below code
-> Create  Properties class object   
           Properties pro = new Properties();
-> Pass the path of properties file  and load the data of properties file to the  Properties class using  load() method. 
          FileInputStream file = new FileInputStream(“path of properties file”); 
          pro.load(file);
-> Use getProperty() method to get data from properties file.
         pro.getProperty(“gmail”);  (“gmail” is the object name that is  created in properties file.)

To create a property file in project
Go to package >New >File 

Create  a file  with .properties as extension and click on finish button.

 

Properties file will look like below image in project structure after created.

properties file in project structure

Store the data in properties file like below image

Store data in properties file

 

Code to read data from properties file

 

How to read data from XML file

Xml file is a simple plain text file with .xml as extension.We can use xml file as Object Repository to store the path of webelements of a page.
So to use xml file as object repository we will follow the below steps.
1.Create a xml file in the project
2.Store the locators path in the xml file
3.Write the script to fetch  the data from xml file

To create a xml file
Go to Package->New->File

Xml File Creation

Create a  file  with .xml as extension and click on finish button 

 

Set file name

Store different path of webelements  in the xml file like below format

<?xml version=”1.0″ encoding=”UTF-8″?>
<locator>
<gmail>//a[text()=’Gmail’]</gmail>
<image>//a[text()=’Images’]</image>
<search>//input[@name=’q’]</search>
</locator>

Code to fetch the data from XML file

This Article is TAGGED in , , . BOOKMARK THE permalink.

Babita Sahu
Automation Software Test Engineer. Skills : Selenium , STLC , Java, Manual Testing , UI / UX Testing

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">