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.
How to create properties file?
Navigate to File menu -> New -> Click on File
The only thing we need to do is, give the file name and give the extension as '.properties'. (Example: dataFile.properties)
Comments
Hi, Can you please show me
Hi, Can you please show me the sample .Property file.
Just create a file and some
Just create a file and some data shown like
URL=http://gmail.com
Username=testuser
Password=password123
Thanks for providing good article.
Thanks for providing good article.
Add new comment