Java properties file is used to store project configuration data or settings. In this blog, we will show you how to read and write to/from a properties file.
In this blog we are going to read username and password from properties file(.config)
Step 1 :- Create config.properties file in Java project
Step 2:- Write some data in config.properties file
Step 3 :- Create Java class to read properties file
Example how preform login using properties file in Seleniuim :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ReadProper
{
public static Properties prop;
public static void main(String[] args) throws Exception
{
//File file = new File("D://config.properties");
//OR
File file = new File(System.getProperty("user.dir")+"//src//config//config.properties");
//OR we can use this line
//FileInputStream file= new //FileInputStream(System.getProperty("user.dir")+"//config//config.properties");
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(file);
//Create properties Class object
prop = new Properties();
prop.load(fileInput);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("URL ::" + prop.getProperty("URL"));
System.out.println("User name::" +prop.getProperty("UserName"));
System.out.println("Password::" +prop.getProperty("Password"));
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(prop.getProperty("URL"));
driver.findElement(By.id("Email")).sendKeys(prop.getProperty("UserName"));
driver.findElement(By.xpath(".//*[@id='next']")).click();
driver.findElement(By.id("Passwd")).sendKeys(prop.getProperty("Password"));
driver.findElement(By.id("signIn")).click();
}
}
In this blog we are going to read username and password from properties file(.config)
Step 1 :- Create config.properties file in Java project
Navigate to File menu -> New -> Click on File
Step 2:- Write some data in config.properties file
Step 3 :- Create Java class to read properties file
Example how preform login using properties file in Seleniuim :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ReadProper
{
public static Properties prop;
public static void main(String[] args) throws Exception
{
//File file = new File("D://config.properties");
//OR
File file = new File(System.getProperty("user.dir")+"//src//config//config.properties");
//OR we can use this line
//FileInputStream file= new //FileInputStream(System.getProperty("user.dir")+"//config//config.properties");
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(file);
//Create properties Class object
prop = new Properties();
prop.load(fileInput);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("URL ::" + prop.getProperty("URL"));
System.out.println("User name::" +prop.getProperty("UserName"));
System.out.println("Password::" +prop.getProperty("Password"));
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(prop.getProperty("URL"));
driver.findElement(By.id("Email")).sendKeys(prop.getProperty("UserName"));
driver.findElement(By.xpath(".//*[@id='next']")).click();
driver.findElement(By.id("Passwd")).sendKeys(prop.getProperty("Password"));
driver.findElement(By.id("signIn")).click();
}
}



Very informative! Our Dell boomi course
ReplyDeletefocuses on integration, cloud automation, and practical experience.
Well explained! A dell boomi training online
ReplyDeleteprepares you to design, deploy, and manage integrations effectively.
Great insights! A practical dell boomi training cost
ReplyDeletehelps in building scalable and reliable integration solutions.
Project-based learning sessions included in these devops training courses help learners improve practical deployment techniques and infrastructure management understanding effectively.
ReplyDeleteGreat post. Students and professionals searching for interactive learning environments often enroll in MuleSoft classes to understand real-time implementation techniques and integration best practices.
ReplyDeleteVery insightful article. Choosing the Best Power BI Training Online can help learners build strong business intelligence skills, gain practical experience, and prepare for certifications that are highly valued by employers across industries.power bi live training
ReplyDelete