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
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.
Read Property file with two values i.e. Locator Type and Locator
Hi Could you please provide code to read following property file.
#ifame locator
iFrame=className=demo-frame
# Date Text Box Locator
DateTextBox=id=datepicker
#Date picker from calender
datePicker=xpath="//td[not(contains(@class,'ui-datepicker-other-month'))]/a[text()='"+value+"']"
Good for Beginners like ME !
Thank You !
Is it possible to use the
Is it possible to use the above same in Mac
Hi,
Hi,
If the java file and properties file are in the same package(i.e. folder),the path has to be given or not?
How to read properties file dynamically
Hi,
The Example shows how to user properties file for one page. Its easy when we have unique keys. But in real time scenario when we handle multiple pages which might have same keys (e.g. button) then I think we need to create different properties file per page. In that case can you please suggest me how to read properties file dynamically according to page???
mozilla browser declaration in property file
mozilla browser declaration in property file .... Please help me to write a syntax for it
Nice article
Thanks ! It is very much clear. Keep the good work going
Add new comment