Read data from Properties file using Java Selenium

In Selenium .properties files are mainly used to store GUI locators / elements, and also Global fields like database configuration details

'.properties' files are mainly used in Java programs to maintain project configuration data, database config or project settings etc. Each parameter in properties file are stored as a pair of strings, in key and value format, where each key is on one line. You can easily read properties from some file using object of type Properties.

Below is a example program which demonstrate to read the data from .properties file using Java.

import java.io.File;
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.firefox.FirefoxDriver;

public class ReadFileData {
	
  public static void main(String[] args)  {
	  File file = new File("D:/Dev/ReadData/src/datafile.properties");
	  
		FileInputStream fileInput = null;
		try {
			fileInput = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Properties prop = new Properties();
		
		//load properties file
		try {
			prop.load(fileInput);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		WebDriver driver = new FirefoxDriver();

		driver.get(prop.getProperty("URL"));
		driver.findElement(By.id("Email")).sendKeys(prop.getProperty("username"));
		driver.findElement(By.id("Passwd")).sendKeys(prop.getProperty("password"));
		driver.findElement(By.id("SignIn")).click();
		
		
		System.out.println("URL ::" + prop.getProperty("URL"));
		System.out.println("User name::" +prop.getProperty("username"));
	    System.out.println("Password::" +prop.getProperty("password"));
  }
}

The below is the Output after executing the program: We are passing the properties values to the webdriver and printing the values at end

URL ::http://gmail.com
User name::testuser
Password::password123

We can also get all the key value pairs in the properties file using the below java program:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class ReadFileData {
	
	public static void main(String[] args) {
		
			File file = new File

("D:/Dev/ReadData/src/datafile.properties");
			FileInputStream fileInput = null;
			try {
				fileInput = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			Properties prop = new Properties();
			try {
				prop.load(fileInput);
			} catch (IOException e) {
				e.printStackTrace();
			}

			Enumeration KeyValues = prop.keys();
			while (KeyValues.hasMoreElements()) {
				String key = (String) KeyValues.nextElement();
				String value = prop.getProperty(key);
				System.out.println(key + ":- " + value);
			}
	}
}

We have not used individual property name to get the KeyValue in the above program. It will get all the key values which are available in the .properties file
The Output of the above program:

URL:- http://gmail.com
password:- password123
username:- testuser

The below is the sample properties file that was used for the above tests. Remember white space between the property name and property value is always ignored. The following is the example.
username=testuser
username = testuser

sample properties file

How to create properties file?
Navigate to File menu -> New -> Click on File

properties file new

The only thing we need to do is, give the file name and give the extension as '.properties'. (Example: dataFile.properties)

create properties file

Selenium Tutorials: 

Comments

Hi Can you please send me that code early. Read data from .txt file with valid and invalid username and password using selenium WebDriver and those script url will be gmail.com. and all valid and invalid pass there in gmail.com.

driver.findElement(By.id("Email")).sendKeys(prop.getProperty("username"));

if i use the above, "the method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)" alert is getting displayed.
please help me.

Well documented and easy to understand.

Hi,
I’m just wondering if we can use a variable in the properties file , i mean something like nth-of-type(” + i+ “) as part of selector?

Thank you!

It worked....nice article.

you declared Properties prop = new Properties(); variable. Hwo to use the Prop variable in multiple @Test cases? I tried using in another and it gives an error .

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.