According to name, a Properties file is used to define properties of keys which would have been used in our Selenium project. A Property file contains data in Key=Value format. It helps external users to communicate with the code and generate output on the basis of inputs.
Features of a Property File:
- Can be used to access user input
- Can be used to store XPath values
- Changes can be made easily without accessing class files
- A user can create as many as properties file as per requirement of Project.
- Easy to maintain test data or user input
- Used in the framework to keep the similar types of variable in separate files.
- It stores data in the Key=Value format without any special characters like “” or ; etc.
- By default property file returns key values in String format.
- Can store values of any data type in the properties file but we have to parse the values in required data type.
- We can also set multiple values for a single key using any separator
- Comments can be included in the file using double hash(#) symbols.
Creating Properties File:
- Create Project
- Now right-click on a folder
- Select New -> File
- Give File name as “testdata.properties”
- Click Finish
- Testdata.properties file would be added to your selected folder.
- Now open the properties file to add key=values in following format:
testURL=http://www.allinoneblogs.com/
searchXpath=//*[@id=”search-3″]/form/label/input
Accessing Properties file in Class files:
Step #1: Locating properties file in project:
File file=new File(filePath);
Step #2: Convert file in byte form using FileInputStream
FileInputStream fis=newFileInputStream(file);
Step #3: Create object of Properties Class
Properties prop=new Properties();
Step #4: Load data from inputstream to properties object using load()
prop.load(fis);
Step #5: Accessing values from Property file to a variable in Class file
String testData=prop.getProperty(“testURL”);
driver.get(testData);
Example 1:
Properties File: (objectRepository.properties)
testURL=http://www.allinoneblogs.com/
searchXpath=//*[@id="search-3"]/form/label/input
Sample.java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Sample {
public static void main(String[] args) throws IOException
{
// Step #1: Locate Properties File
File file=new File("configuration//objectRepository.properties");
// Step #2: Convert data into bytes
FileInputStream fis=new FileInputStream(file);
// Step #3: Creating object of Properties Class
Properties prop=new Properties();
// Step #4: Load data from inputstream to Properties object
prop.load(fis);
// Step #5: Store values from Properties file to local variable.
String URL=prop.getProperty("testURL");
String Xpath=prop.getProperty("searchXpath");
System.out.println("URL: "+URL);
System.out.println("Xapth: "+Xpath);
}
}
Example 2:
- This program contains multiple Properties File
- Store and access multiple values using single key.
- Store and access numeric value
objectRepository.properties
testURL=http://www.allinoneblogs.com/
searchXpath=//*[@id="search-3"]/form/label/input
userInputs.properties
multipleSearchValues=java,filehandling,selenium,automation
keywordsCount=4
TestFile.java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestFile {
public static void main(String[] args) throws IOException, InterruptedException
{
System.setProperty("webdriver.chrome.driver","drivers//chromedriver.exe");
WebDriver driver=new ChromeDriver();
// loadPropertyFile is a user defined method to locate multiple properties file
Properties configProp=loadPropertyFile("configuration//objectRepository.properties");
Properties userInputs=loadPropertyFile("configuration//userInputs.properties");
// Access and Store values from Properties File to local variables.
String testURL=configProp.getProperty("testURL");
String searchXpath=configProp.getProperty("searchXpath");
driver.get(testURL);
String searchValues=userInputs.getProperty("multipleSearchValues");
// Convert input string in an array, based on any unique character
String[] testArray=searchValues.split(",");
for (String keyword : testArray)
{
driver.findElement(By.xpath(searchXpath)).clear();
driver.findElement(By.xpath(searchXpath)).sendKeys(keyword);
driver.findElement(By.xpath(searchXpath)).sendKeys(Keys.ENTER);
Thread.sleep(1000);
}
driver.close();
// Following Statement will throw "Type mismatch" error
// int numericValue=userInputs.getProperty("keywordsCount");
// Alternate method to access numeric values
String numericValue=userInputs.getProperty("keywordsCount");
int count=Integer.parseInt(numericValue);
System.out.println("Keyword Count: "+count);
}
public static Properties loadPropertyFile(String filePath) throws IOException
{
// Loading Properties file
File file=new File(filePath);
FileInputStream fis=new FileInputStream(file);
Properties prop=new Properties();
prop.load(fis);
return(prop);
}
}
Thanks Ashok,
The Way you explain it by step by step process is very useful for me.
Please write a blog on “How to write properties file ?”
Hi Devi,
Correct me if I am wrong, I think you need help to enter values in the into the properties file. I have already mentioned the same under “Creating Properties File” section:
Create Project
Now right-click on a folder
Select New -> File
Give File name as “testdata.properties”
Click Finish
Testdata.properties file would be added to your selected folder.
Now open the properties file to add key=values in following format:
testURL=http://www.allinoneblogs.com/
searchXpath=//*[@id=”search-3″]/form/label/input
Please let me know if any other support required.
Thanks!
Hi,
I think he meant that extract the locators from the properties file and use the test data say from CSV file using dataProvider in testNG