Object Repository in Selenium - Reading And Writing Properties in Selenium

10:39 PM 0 Comments

In Selenium Webdriver if we need to store the Object Properties, we can use Properties file where we can store both the Property name and Value.

Property Name : Key
Property Value : Value

Suppose if am able to find the text box in http://www.google.com using the following properties.
In the below screenshot the Property Name is 'id' and Property Value is 'gs_taif0'. So, in future if developer changes these properties, then we have to modify the Selenium WebDriver Script. Instead of that,

we can maintain this in Properties file.
Property Name : id
Property Value :  gs_taif0












Steps for Creating the Property File:
1. Open Notepad.
2. Write the following Text as shown below:













3.Copy the file 'Google.properties' in the project Folder. Suppose i create a java project with name as 'Selenium Project' then we have to copy the property file in the project folder as shown below:












Steps for Writing a Java Program to Read the property value in Selenium WebDrvier Script

Before we get into the program we need to understand the JAVA API(Application Programming Interface).

Java provides an java.util.Properties class which helps us to read the property values and update the property values.

Please find the methods below which helps us in reading and writing the property values:

1. loadReads a property list (key and element pairs) from the input byte stream.


voidload(InputStream inStream)
Reads a property list (key and element pairs) from the input byte stream.

2. setProperty: Useful for setting values for the Key.

ObjectsetProperty(String key, String value)
Calls the Hashtable method put.

3. getProperty: Searches for the property with the specified key in this property list.


StringgetProperty(String key)
Searches for the property with the specified key in this property list.

Program for Reading the Property file:


import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ReadPropertyFile {

public static void main( String[] args ) throws IOException{

FileInputStream fis = new FileInputStream("Google.properties");
Properties prop = new Properties();
prop.load(fis);
//Reading the property values
System.out.println(prop.getProperty("search_textbox_id"));


WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.id(prop.getProperty("search_textbox_id")));


}
}














The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Unknown

This site creates a platform to become an expert by gaining knowledge of unknown real-time issues as well.

0 comments: