'.properties' files have configuration data, database config or project settings etc to maintain project in Java. Each parameter in properties file are stored as a pair of strings, in key and value format and key is case sensitive, where each key is on one line. You can easily read properties from some file using object of type Properties.
In Selenium .properties files are mainly used to store GUI locators / elements, and also Global fields like database configuration details.
The sample properties file that was used for the java program
Here is a example program which demonstrate to read the data from .properties file using Java.
In Selenium .properties files are mainly used to store GUI locators / elements, and also Global fields like database configuration details.
The sample properties file that was used for the java program
Here 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.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Readdatafrompro {
public static void main(String[] args) {
try {
// Create FileInputStream Object
FileInputStream fileInput = new FileInputStream(new File("data.properties"));
// Create Properties object
Properties prop = new Properties();
//load properties file
prop.load(fileInput);
//// WebDriver reference but Firefox object
WebDriver driver = new FirefoxDriver();
//Maximize browser window
driver.manage().window().maximize();
//Visit to Facebook
driver.get(prop.getProperty("URL"));
//Read Username from Properties and send to facebook Username text field
driver.findElement(By.id("email")).sendKeys(prop.getProperty("Username"));
//Read Password from Properties and send to facebook password text field
driver.findElement(By.id("pass")).sendKeys(prop.getProperty("Password"));
driver.findElement(By.id("loginbutton")).click();
if (driver.getPageSource().contains("Please re-enter your password")) {
System.out.println("--- Pass ---");
} else {
System.out.println("!!!! Fail !!!");
}
//Print Properties Values
System.out.println("URL =: " + prop.getProperty("URL"));
System.out.println("User name =: " + prop.getProperty("Username"));
System.out.println("Password =: " + prop.getProperty("Password"));
// Browser close
driver.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchElementException e) {
e.printStackTrace();
}
}
}
I am getting a null while the URL is passed. User name & Password is getting passed.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHelpful, thanks!
ReplyDeleteGreat blog.well explained..Thanks for sharing..keep update selenium training in chennai selenium training in velachery
ReplyDelete