TechBeamers
    • Python
      • Python Basic
      • Python OOP
      • Python Quizzes
      • Python Examples
      • Python Selenium
      • Python Advanced
    • Java
      • Java Basic
      • Java Flow Control
      • Java OOP
      • Java Quiz
      • Java Interview
    • C
    • C#
    • SQL
    • MySQL
    • Selenium
      • Selenium Tutorial
      • TestNG Tutorials
      • Selenium Interview
      • Selenium Quiz
    • Testing
      • Software Testing
      • Manual Testing
      • Software Testing Quiz
      • Testing Interview Questions
    • Agile
    • More
      • Android
      • Angular
      • Linux
      • Web Dev
      • Best IDEs
      • Front-End Quiz
      • ShellScript Quiz
      • C++ Quiz
      • IOT
      • General
    TechBeamers
    Selenium Tutorial

    Implement Object Repository In Selenium WebDriver

    Updated:September 12, 2023
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Selenium Webdriver is the most used software automation testing tool. It provides APIs that operate on the web objects using their element locators like ID, CSS, XPath, etc. It doesn’t have any built-in feature to implement an object repository.

    Lacking this facility increases the efforts of maintaining the object locators. If you hard code these elements locators in the source code that makes it more unmanageable. Also, this approach leads to frequent code changes whenever the locator gets changed. Hence, test automation doesn’t get fixed until the UI gets finalized.

    If you are using an Id (which is one of the types of element locators) in multiple test cases and it gets modified. Then, you would need to update its value in all test cases. Unfortunately, if you miss updating any of them, then the test case eventually will fail.

    Using Properties File in Selenium

    Benefits of Using Properties File

    You can overcome all of such issues with the help of an object repository. You can implement the object repository using Java’s Properties class which is a part of the Java Util package. You can store the element locators as the key-value pair in a property file. You can even use it to hold project configuration properties like usernames/passwords etc.

    Username_field =name:username
    Password_field =name:password
    Login_button =classname:loginbtn
    url=http://phptravels.net/login
    username=user@phptravels.com
    password=demouser
    

    Let’s now check out how can we implement an object repository using the properties file in Selenium WebDriver.

    Firstly, we’ll see one of the simplest methods to create a property file. Just make sure you have Eclipse installed on your system. We’ve tested the example code using the Eclipse IDE.

    Implement Object Repository from a Properties File

    It can be quickly done using the Eclipse IDE. Open the Eclipse and navigate to File Menu >> New >> Click on the File option.

    Implement Object Repository
    Implement Object Repository – Select a file.

    The only thing you need to do is to set the file name and add the extension as <.properties>. (Example: dataFile.properties)

    Implement Object Repository
    Implement Object Repository – Specify File Name

    Now you can open it in the Eclipse using the Property file editor where you can add/edit any key-value pair. Though, it is a simple text file that you can modify using basic file editors like Notepad.

    Read a Properties File

    In Selenium projects, the main purpose of the “.properties” files is to store the GUI locators/elements, project configuration data, database configuration, etc.

    Each parameter in the properties file appears as a pair of strings, in key-value format, where each key is on one line followed by its value separated by some delimiter. You can refer to the sample properties file from the previous section.

    Below is an example program that demonstrates to read the data from the .properties file using Java.

    To execute the sample code in Java Eclipse, create a package named ‘seleniumObjectMap’ and add the class file ‘ReadFileData’ to this package. Also, create a folder named <Resources> and add a file named <datafile.properties> in this folder. Please see the attached snapshot below which provides a glimpse of the folder structure used for the sample project.

    Make sure that you add Selenium Java Webdriver jar files as referenced in the project build path. For details on how to create a Selenium test project refer to our -> post on “Six Steps to Make a Selenium Project“.

    Implement Object Repository
    Implement Object Repository – Reading properties file.
    v
    Implement Object Repository – Reading datafile.properties

    Sample Source Code

    package seleniumObjectMap;
    
    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 ReadFileData {
    
    	public static void main(String[] args) {
    		final String propFile = System.getProperty("user.dir")
    				+ "\\resources\\datafile.properties";
    		File file = new File(propFile);
    		FileInputStream fileInput = null;
    		try {
    			fileInput = new FileInputStream(file);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    		Properties prop = new Properties();
    		try {
    			prop.load(fileInput);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		WebDriver driver = new FirefoxDriver();
    		driver.get(prop.getProperty("url"));
    		driver.manage().window().maximize();
    		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    		driver.findElement(By.name("username")).sendKeys(
    				prop.getProperty("username"));
    		driver.findElement(By.name("password")).sendKeys(
    				prop.getProperty("password"));
    		driver.findElement(By.className("loginbtn")).click();
    
    		System.out.println("URL ::" + prop.getProperty("url"));
    		System.out.println("User name::" + prop.getProperty("username"));
    		System.out.println("Password::" + prop.getProperty("password"));
    
    	}
    
    }

    We passed the property values to the Webdriver and printed the values at the end. Check the below Output after executing the above program.

    url=http://phptravels.net/login
    username=user@phptravels.com
    password=demouser
    setup selenium webdriver project in eclipse using properties file in selenium
    Previous ArticleHow to Create TestNG Test Case in Selenium
    Next Article How to Read data from Properties File Using TestNG
    Join Us!
    Loading
    Latest Tutorials

    7 IoT Trends to Watch in 2023

    September 23, 2023

    SQL Table Creation: The Missing Manual

    September 15, 2023

    Python Print() with Examples

    September 25, 2023

    String concatenation in Python Explained

    September 24, 2023
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    • Terms of Use
    © 2023 TechBeamers. All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.