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.
Store the data in properties file like below image
Code to read data from properties file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package selenium; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class PropertyDataRead { public static void main(String[] args) throws IOException, InterruptedException { System.setProperty("webdriver.chrome.driver", "path of chrome driver"); WebDriver driver=new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.get("https://www.google.com"); Properties pro =new Properties(); FileInputStream file=new FileInputStream("D:\\selenium\\New\\readProperty.properties"); pro.load(file); driver.findElement(By.xpath(pro.getProperty("gmail"))).click(); driver.navigate().back(); driver.findElement(By.xpath(pro.getProperty("image"))).click(); driver.navigate().back(); Thread.sleep(5000); WebElement searchButton=driver.findElement(By.xpath(pro.getProperty("search"))); searchButton.sendKeys("selenium" ,Keys.ENTER); } } |
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
Create a file with .xml as extension and click on finish button
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package selenium; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ReadDataFromXml { public static String readXmlData(String data) throws ParserConfigurationException, SAXException, IOException { File file = new File("D:\\selenium\\New\\ObjectDataRead.xml"); DocumentBuilderFactory Df = DocumentBuilderFactory.newInstance(); DocumentBuilder Db = Df.newDocumentBuilder(); Document document = Db.parse(file); NodeList list = document.getElementsByTagName("locator"); Node node1 = list.item(0); Element ele = (Element) node1; return ele.getElementsByTagName(data).item(0).getTextContent(); } public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { System.setProperty("webdriver.chrome.driver", "path of chrome driver"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.get("https://www.google.com"); driver.findElement(By.xpath(readXmlData("gmail"))).click(); driver.navigate().back(); driver.findElement(By.xpath(readXmlData("image"))).click(); driver.navigate().back(); driver.findElement(By.xpath(readXmlData("search"))).sendKeys("Selenium", Keys.ENTER); } } |