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();
}
}
Comments
Post a Comment