Learn Selenium and Java for Selenium

Sunday, 1 May 2016

Creating Object Repository Using Properties file in Selenium Webdriver

What are Properties file and why we need it?

In Selenium web driver there is not any build in facility to create object repository, so maintenance of page objects becomes very hard if we write all objects of page on code level.So we can create Properties file support of java software in order to store the object X-Path/Id of a location of web element in this file

How to create a Properties file?

Step 1-Right click on project package ->New->Other



Step 2 :- On New Wizard window ,expand general Folder and select file and click on Next Button

Step 3:- Give the file name as Object.Properties and click on Finish button, which will add the Properties file under a Package



Step 4:

Double Click  on Properties file to add the X-Path of the "Login" button of the Facebook page

X-path of Login button of Facebook


The below code Read X-Path of "Login" button from the Properties file and then click on "Login" Button of Facebook

 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.util.Properties;  
 import org.apache.commons.io.FileUtils;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import org.testng.annotations.Test;  
   
   
   
 public class ReadPropertiesfile {  
        
 public static void main(String[] args) throws IOException {  
             
 WebDriver a= new FirefoxDriver();  
 a.manage().window().maximize();  
 a.get("http://www.facebook.com");  
             
 //Create Object of Properties Class.  
 Properties obj = new Properties();   
 //Create Object of FileInputStream Class. Pass file path -  
 FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\src\\Object.Properties");   
 //Pass object reference objfile to load method of Properties object  
  obj.load(objfile);  
              
 //Accessing element locators of all web elements using obj.getProperty(key)  
 a.findElement(By.xpath(obj.getProperty("Sumbit"))).click();   
 a.quit();  
             
 }  
 }  


Notes

1-FileInput Stream is a class offered by java to read data from a file
2- Properties Class offers a load(InputStream Is) which loads data from InputStream Object which is objfile
3- getProperty("user.dir") will return the current project directory/ to get the path of the workspace

Output



0 comments:

Post a Comment

Popular Posts

Recent Posts



Newsletter

Join our newsletter to get secret tips directly in your inbox!


*We Hate Spam!

Powered by Blogger.