You are reading the article Object Repository In Selenium (Xml &Amp; Properties File) updated in September 2023 on the website Chivangcangda.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Object Repository In Selenium (Xml &Amp; Properties File)
What is an Object Repository?An object repository is a common storage location for all objects. In Selenium WebDriver context, objects would typically be the locators used to uniquely identify web elements.
In this tutorial, you will learn-
Types of Object Repositories in Selenium Web DriverSelenium WebDriver does not offer an in-built object repository by default. However, object repositories can be built using the key-value pair approach wherein the key refers to the name given to the object and value refers to the properties used to uniquely identify an object within the web page.
The following are the types of object repositories that can be created in Selenium WebDriver.
Object Repository using Properties file
Object Repository using XML file
Selenium Web Driver Object repository using Properties fileIn this approach, properties file is a text file wherein data is stored on the form of key-value pairs. The below tutorial will address the following topics.
Step 1) Creating a properties file in eclipse
To start with, the below java project structure needs to be created in eclipse. Project name and package name can be any valid names.
A file named ‘application.properties’ must be displayed on Project Structure
Step 2) Storing data onto properties file
Data is stored in properties file in the form of key-value pairs, with the key being unique across the file.
We will try to use the properties file to identify webelements using locator values.
Open application.properties file in Eclipse and store the following data
MobileTesting=//a[text()='MOBILE TESTING'] EmailTextBox = philadelphia-field-email SignUpButton = philadelphia-field-submit
Navigate back
Enter data onto email textbox using ID
Step 3) Reading data from properties file
Reading data from properties file can be done using the built-in Properties class provided in chúng tôi package.
Initially, an object of Properties class need to be created as below
Properties obj = new Properties();We need to create an object of FileInputStream class with the path to properties file
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\application.properties");Reading data from properties file can be done using load method offered by Properties class in java. The below code demonstrates the usage of load method.
Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\application.properties"); obj.load(objfile); String mobileTesting = obj.getProperty("MobileTesting");The string ‘mobileTesting’ will contain the XPATH to identify the Mobile Testing link within the webpage.
Step 4) Using properties file in test scriptsProperties file can be used in test scripts by reading data from a properties file and passing the data as a parameter to the findElement method. The below code demonstrates the usage of data read from properties file in test scripts.
The below is the complete code used for the above test scenario.
package com.objectrepository.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class DemoOR { public static void main(String[] args) throws IOException { WebDriver driver; System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\application.properties"); obj.load(objfile); driver.navigate().back(); } } Selenium WebDriver Object Repository Using XML FileXML stands for Extensible Markup Language. An XML File uses Document Object Model(DOM) as the basic structure. XML File format will replicate the HTML format upon which the webpage is constructed. Below is the list of topics that will be covered.
Step 1) Creating an XML file in eclipse
The below java project structure needs to be created in Eclipse.
An XML file will be added to the project folder as shown below
Step 2) Storing data onto XML fileData can be stored in XML file in the form of Document Object Model (DOM). For simplicity sake, we can use the below test scenario as an example.
Navigate Back to Home page
Enter data onto email textbox using ID
The below is the format of XML File to be used.
Store the above XML code in properties.xml
In the design tab you will see
Step 3) Reading data from XML file1. Reading data from XML file can be accomplished using the built-in ‘dom4j’ class in java. Please note that you need to add the below JAR files into the buildpath of your project before proceeding with the code.
jaxen.jar
dom4j-1.6.jar
2. Below is the code to read data from XML file.
File inputFile = new File(System.getProperty("user.dir") +"\properties.xml"); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(inputFile); String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText(); String emailTextBox = document.selectSingleNode("//menu/email").getText(); String signUpButton = document.selectSingleNode("//menu/signup").getText();3. Initially, we need to create a File object and pass it as a parameter to the ‘read’ method of SAXReader class. Once the XML file data is read successfully, we can access individual nodes of XML document using the ‘selectSingleNode’ method.
Step 4) Using XML file in test scriptsXML file can be used in test scripts by reading data from XML file and passing the data as parameter to the findElement method. The below code demonstrates the usage of data read from XML file in test scripts.
The below code demonstrates the use of XML file in selenium WebDriver
package com.objectrepository.demo; import java.io.*; import java.util.*; import org.dom4j.*; import org.dom4j.io.SAXReader; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class DemoORXML { public static void main(String[] args) throws DocumentException { WebDriver driver; System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); File inputFile = new File(System.getProperty("user.dir") +"\properties.xml"); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(inputFile); String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText(); String emailTextBox = document.selectSingleNode("//menu/email").getText(); String signUpButton = document.selectSingleNode("//menu/signup").getText(); driver.navigate().back(); } }Download the WebDriver Eclipse Project
Summary:
An object repository is a common storage location for all objects
Selenium WebDriver does not offer an in-built object repository by default
You can create 2 Types of Object Repository in Selenium
Object Repository using Properties file
Object Repository using XML file
The properties file is a text file wherein data is stored on the form of key-value pairs
XML File format will replicate the HTML format upon which the webpage is constructed.
You're reading Object Repository In Selenium (Xml &Amp; Properties File)
Update the detailed information about Object Repository In Selenium (Xml &Amp; Properties File) on the Chivangcangda.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!