Data Driven using Properties files in Selenium webdriver
- qatestingthreesixt
- Dec 1, 2021
- 1 min read
Most of the time, we need to test our script in different environment like test, Production and with different login credentials.

To achieve this we need to get the urls and login credentials from a variable data instead of making a whole script customization. Similarly we could have different purpose and tweaks for data driven. Even we can connect the data source using spreadsheets, data connectors etc. Here we have one more option for small data handling.
Here is the step by step Procedure which gives an idea on how to drive login credentials text boxes with different sets of data from a properties file: 1.Create a Java Project in Eclipse. 2.Right click on the Source folder , go to File >> New >> File . 3.Create a new file named data.properties. 4. Insert following data into it:
username=user@email.com password=abcd@123
5. Change your code accordingly and Run.
CODE:
public class datapro { public WebDriver driver;
@BeforeTest public void login() { // TODO Auto-generated method stub
System.setProperty(“webdriver.chrome.driver”,
“D:\\Tools\\BrowserDriver\\chromedriver.exe”);
driver=new ChromeDriver();
//implicit wait driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(“website url”); // driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.manage().window().maximize(); System.out.println(driver.getTitle());
}
@Test public void throughfile() throws IOException{
Properties prop= new Properties();
FileInputStream fis=new FileInputStream(“path of properties file\\src\\data.properties”);
prop.load(fis);
driver.findElement(By.xpath(“.//*[@id=’nav-login’]/li[1]/a”)).click();
driver.findElement(By.xpath(“.//* [@id=’inputEmail’]”)).sendKeys(prop.getProperty(“username”)); driver.findElement(By.xpath(“.//* [@id=’inputPassword’]”)).sendKeys(prop.getProperty(“password”)); driver.findElement(By.xpath(“html/body/form/div/div/div[1]/div/div/button[2]”)).click();
}
}
Comments