Read data from Properties file using Java Selenium webdriver

Read data from Properties file using Java Selenium webdriver,We can easily read properties from object repository file or some other files using object type properties,In this post example you will learn perfect knowledge about Read data from Properties file using Java Selenium.Please Read Selenium WebDriver Tutorial.
Properties files are using in Java programming which is mainly using for configuration data in their project like data base configuration details,GUI Element data etc.In Selenium Test scripting you can use properties files to read the data from files to your Test scripts in order to maintain clean scripts and clear understanding of test scripts.
Read data from Properties file using Java Selenium webdriver
Properties file in Selenium WebDriver

Read data from Properties file using Java Selenium

In below example you will understand how to create properties file and read data from properties files ,please read full article to understand clearly and i have given brief explanation below the end of the code with each step so that you people can understand clearly.Let’s see the below code.

package com.eu.examples;
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;
import org.testng.annotations.Test;

public class PropertiesFilesSelenium {


 //Read data from Properties file using Java Selenium
 //OR= Object Repository:
 public static WebDriver driver;

 @Test
 public void openBrowser() throws FileNotFoundException{


 driver = new FirefoxDriver();
 //using properties to create object
 Properties property = new Properties();
 //Call the file OR.propoeties file using FileInputStream
 FileInputStream objFile = new FileInputStream("E:\QTP Tutorials\Selenium Automation\ExtentUsages\OR.properties");

 //Load the properties File
 try {

 property.load(objFile);

 } catch (IOException e) {

 System.out.println(e.getMessage());
 e.printStackTrace();

 }
 //Open Software Testing blogspot
 driver.get(property.getProperty("baseURL"));
 //Maximize the window
 driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 System.out.println("Window is maximized");
 //Click on Manual Testing menu
 driver.findElement(By.xpath(property.getProperty("Manualtesting"))).click();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 System.out.println("Clicked on Manual Testing menu");
 //Click on QTP Tutorials
 driver.findElement(By.xpath(property.getProperty("QTPTutorials"))).click();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 System.out.println("Clicked on QTP Testing menu");
 //Click on SeleniumTutorials
 driver.findElement(By.xpath(property.getProperty("SeleniumTutorials"))).click();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 System.out.println("Clicked on sELENIUM Testing menu");
 //Click on LoadrunnerTutorials
 driver.findElement(By.xpath(property.getProperty("LoadrunnerTutorials"))).click();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 System.out.println("Clicked on Loadrunner Testing menu");

 }

OUTPUT:

Once you run above program you will get below output in Eclipse console

ALSO READ  How to take screenshot in Selenium IDE

Window is maximized
Clicked on Manual Testing menu
Clicked on QTP Testing menu
Clicked on sELENIUM Testing menu
Clicked on Loadrunner Testing menu
PASSED: openBrowser

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

Properties File

#Software Testing Application Object Properties

baseURL=http://softwaretestutorials.blogspot.in

#Different Menu's Properties
Manualtesting=/html/body/div[4]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/div/ul/li[2]/a
QTPTutorials=/html/body/div[3]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/div/ul/li[3]/a
SeleniumTutorials=/html/body/div[3]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/div/ul/li[4]/a
LoadrunnerTutorials=/html/body/div[3]/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/div/ul/li[5]/a

Code Explanation:

Below details will give you brief details about above code in detail explanation,let’s start understanding the code.

ALSO READ  Selenium WebDriver Scripting Guidelines

Properties property = new Properties();
In Above i have created one object under Properties() because wit this object you can load or read the properties file easily.

FileInputStream objFile = new FileInputStream(“E:\QTP Tutorials\Selenium Automation\ExtentUsages\OR.properties”);

FileinputStream in java is useful in reading the different files in order to get the data from specified file and here we are going to use this FileInputStream to load the file into our scripts.

ALSO READ  Selenium Webdriver Using Python - Complete Tutorial Guide

try {
property.load(objFile);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

In order to load the FileinputStream file ,you can use above try catch block for load the file in order to avaoid IOException while executing the script.

driver.get(property.getProperty(“baseURL”));

In order to read the data from Propoerties file just you need to use property.getProperty(String Key),here property is created object in properties i.e property and getProperty is a method to read the properties from property files. Thank you for reading please share my post.

Rajesh K

Leave a Reply

Your email address will not be published. Required fields are marked *