TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: How to Implement Object Repository (OR) in Selenium
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

How to Implement Object Repository (OR) in Selenium

Last updated: Jun 03, 2024 1:01 pm
By Meenakshi Agarwal
Share
6 Min Read
Implement Object Repository in Selenium Webdriver
Implement Object Repository in Selenium Webdriver
SHARE

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.

Contents
Benefits of Using Properties FileImplement Object Repository from a Properties FileRead a Properties FileSample Code to Implement Object Repository

Why Implement Object Repository for Selenium Tests

It is a tough task for any test engineer to maintain ever-changing locators in Selenium. If you hard code them in your test cases 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.

Consider that you are using an ID (which is one of the types of element locators) in multiple test cases, and 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.

Benefits of Using Properties File

You can overcome all of the challenges mentioned above with the help of an object repository.

1. Can implement the object repository using Java’s Properties class from the Java util package.
2. Can store the element locators as the key-value pair in a property file.
3. 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.

Create a property file to 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)

save as dataFile.properties
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 how 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“.

Class to read from object Repository
Implement Object Repository – Reading properties file.
save locators in the object repository
Implement Object Repository – Reading datafile.properties

Sample Code to Implement Object Repository

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

Before you leave, render us your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.

Happy testing,
TechBeamers.

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

TAGGED:Setup Selenium Webdriver
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article How to Create TestNG Test Case in Selenium
Next Article Read data from Properties File Using TestNG Framework How to Read data from Properties File Using TestNG

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x
x