Skip to toolbar

Properties File | Accessing properties file for user input or test data.

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.

Properties File

Features of a Property File:
  1. Can be used to access user input
  2. Can be used to store XPath values
  3. Changes can be made easily without accessing class files
  4. A user can create as many as properties file as per requirement of Project.
  5. Easy to maintain test data or user input
  6. Used in the framework to keep the similar types of variable in separate files.
  7. It stores data in the Key=Value format without any special characters like “” or ; etc.
  8. By default property file returns key values in String format.
  9. Can store values of any data type in the properties file but we have to parse the values in required data type.
  10. We can also set multiple values for a single key using any separator
  11. Comments can be included in the file using double hash(#) symbols.
Creating Properties File:
  1. Create Project
  2. Now right-click on a folder
  3. Select New -> File
  4. Give File name as “testdata.properties
  5. Click Finish
  6. Testdata.properties file would be added to your selected folder.
  7. Now open the properties file to add key=values in following format:

testURL=http://www.allinoneblogs.com/
searchXpath=//*[@id=”search-3″]/form/label/input

PropertiesFile

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:
  1. This program contains multiple Properties File
  2. Store and access multiple values using single key.
  3. 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);
	}
}

5535total visits,1visits today

3 thoughts on “Properties File | Accessing properties file for user input or test data.

  1. 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 ?”

    1. 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!

  2. 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

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 14
  • Page views today : 14
  • Total visitors : 48,091
  • Total page view: 66,223

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.