• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / Complete Guide to Set Object Repository in Selenium Webdriver

Complete Guide to Set Object Repository in Selenium Webdriver

January 1, 2019 by Mukesh Otwani 46 Comments

Object Repository in Selenium Webdriver

Whenever you talk about repository by the name itself you can think that it is kind of storage. Object repository is the collection of object and object here is locators. Object Repository in Selenium Webdriver is quite easy to design so let’s get started.

Note- Before we start I also want to highlight that people also use the same concept to store all kind of configuration details as well. 

The example you can keep URL, browser details, some path, port details and so on, in that case, this will behave as the configuration file.


Here locators mean WebElement id, name, CSS, XPath, class name,LinkText
, PartialLinkText, TagName etc.

Object Repository in Selenium Webdriver

To understand the importance of Object repository, we will take a real-time scenario.

The scenario is you have 100 test cases and in all test cases, you have login scenario. If your application changes now and some locators changes like id changes from email to login-email then you have to modify all the test cases and you have to change the id.

This process does not make any sense and this will be a very tedious task, so to overcome with this we will move all locators in a separate file and we will link all test cases to this file. In case any changes happen in our locators then we will simply change in that file, not all our test cases.

This will increase the modularity of test cases and I will strongly suggest that you should use Object Repository in Selenium Automation.

Note-  Many companies also use Page Object Model to store all locators which also make the test in a readable format. Depends on your current framework style you can adopt any of these. Personally, I use Page Object Model using PageFactory which make my test more readable and in terms of performance as well, I felt POM is faster.

I have uploaded the video on same.

 

Object Repository in Selenium Webdriver- Selenium Tutorial

Precondition

1- Project should be created and Selenium jars should be added- Click here to configure .

2- Now create a new file and give file name as Object_Repo (depends on you)with extension .properties

For this right click on project,> create a new file > Specify name

 

Object Repository in Selenium Webdriver

 

Select folder and specify name & extension

Object Repository in Selenium Webdriver

 

3- Now file is created > Double click on file > File will open in Edit mode

Object repository works on KEY and Value pair so we will specify Keys based on our project and in values we will give locator value.

In below example, I have taken XPath for username, password and login button for the facebook login page.

Key you can write based on your project standard, but value should be correct which is coming from the application

facebook.login.username.xpath=.//*[@id='email']
facebook.login.password.xpath=.//*[@id='pass']
facebook.login.Signup.xpath=.//*[@id='loginbutton']

 

 

Object Repository in Selenium Webdriver

 

4- Write Selenium script and use the same in the script

Object Repository in Selenium Webdriver- Selenium Tutorial

package ObjectRepoExample;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TestFacebook {

@Test
public void TestOR() throws IOException{

// Specify the file location I used . operation here because
//we have object repository inside project directory only
File src=new File(".Object_Repo.properties");

// Create  FileInputStream object
FileInputStream fis=new FileInputStream(src);

// Create Properties class object to read properties file
Properties pro=new Properties();

// Load file so we can use into our script
pro.load(fis);

System.out.println("Property class loaded");

// Open FirefoxBrowser
WebDriver driver=new FirefoxDriver();

// Maximize window
driver.manage().window().maximize();

// Pass application
driver.get("http://www.facebook.com");

// Enter username here I used keys which is specified in Object repository.
// Here getProperty is method which
// will accept key and will return value for the same
driver.findElement(By.xpath(pro.getProperty("facebook.login.username.xpath"))).
sendKeys("Selenium@gmail.com");

// Enter password here I used keys which is specified in Object repository.
// Here getProperty is method which
// will accept key and will return value for the same
driver.findElement(By.xpath(pro.getProperty("facebook.login.password.xpath"))).
sendKeys("adsadasdas");

// Click on login button here I used keys which is specified in Object repository.
// Here getProperty is method which
// will accept key and will return value for the same
driver.findElement(By.xpath(pro.getProperty("facebook.login.Signup.xpath"))).click();

}

}

You can also create the reusable method which will accept the key and will return the value.  Hope you liked the post and video, if yes then please share with your friends as well.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

Filed Under: Advance Selenium Tagged With: How to read property file in Selenium webdriver, Object Repository, Object Repository in Selenium Webdriver

Reader Interactions

Comments

  1. Yash says

    February 11, 2020 at 10:57 AM

    Hi Mukesh,

    What is the use of <Span style……. ? Is it necessary to use?

    Regards,
    Yash

    Reply
    • Mukesh Otwani says

      February 11, 2020 at 11:43 AM

      Hi Yash,

      It is an HTML tag. In UI automation there is no strict rule for the usage of any tags while locator selection. You can avoid it if you don’t want to use span tag

      Reply
  2. Tanushree says

    April 17, 2019 at 6:03 PM

    Yes. I am getting nullPointerException.
    File f1=new File(“D:\Selenium\Pulse\src\CCHS_241\Repo.properties”);
    //FileInputStream fis=new FileInputStream(f1);
    Properties pro=new Properties();
    FileReader o1=new FileReader(f1);
    pro.load(o1);

    System.out.println(pro.getProperty(usrname));

    My usermane and password has following attributes:
    name=’username’ and for password its id=’password’
    suggest me the repo file and command to fetch these attributes from repo file

    Reply
    • Mukesh Otwani says

      April 17, 2019 at 6:53 PM

      Hi Tanushree,

      Please don’t mention single quotes anywhere in Repo.properties file. Moreover please use below code

      File f1 = new File(“D:\\Selenium\\Pulse\\src\\CCHS_241\\Repo.properties”);
      FileInputStream fis = new FileInputStream(f1);
      Properties pro=new Properties();
      pro.load(fis);
      System.out.println(pro.getProperty(username));

      Reply
  3. Tanushree says

    April 17, 2019 at 5:07 PM

    Hi Mukesh, I need your help.
    I have created the repository file, but keys are not picked from that file. hence test fails. When I gave direct locator path , test case gets executed euccessfully.
    Please suggest how to resolve issue.

    Reply
    • Mukesh Otwani says

      April 17, 2019 at 5:56 PM

      Hi Tanushree,

      Are you getting NullPointerException in console?

      Reply
  4. mani.bhute@gmail.com says

    February 6, 2019 at 9:50 PM

    Hi Mukesh is making in selenium property fiile for filesd like fisrtname and last name and email,password and signup .
    can we do alternately in selenium by creating a library file and keep all the elements which we used frequentlky?
    should we use both libaray and object repository in selenium?

    Reply
    • Mukesh Otwani says

      February 7, 2019 at 10:49 AM

      Hi Mani,
      It fully depends on how you want to use it. There is no standard defined for any scenarios. Only motto while designing framework are robustness, non redundancy, scalable. If you think that you can us these values multiple times then go ahead with its library creation.

      Reply
  5. Myra Khetpal says

    February 18, 2017 at 12:59 PM

    Hi ,
    Followed the exact steps but the variable pro is returning null. what could be possible debug method?

    Reply
    • Mukesh Otwani says

      February 18, 2017 at 7:12 PM

      Hi Myra,

      Check whether you are providing correct path for file.

      Reply
      • Myra Khetpal says

        February 20, 2017 at 2:07 PM

        Hey thanks Mukesh! Got it correct!
        Do you have any tutorial for log4j ? Could you pls share the URL ??

        Reply
        • Mukesh Otwani says

          February 20, 2017 at 7:31 PM

          Hi Myra,

          Here is the link http://learn-automation.com/how-to-create-log-files-in-selenium/

          Reply
  6. Radha says

    February 17, 2017 at 5:51 AM

    Hi I followed the exact steps in the video, I am unable to open browser and test is saying failed, can you help me how to fix this

    Reply
    • Mukesh Otwani says

      February 17, 2017 at 9:43 AM

      Hi Radha,

      First check whether you are able to read value against key which you have given inside your script. Do print its value and then proceed further.

      Reply
  7. siri says

    February 2, 2017 at 2:48 PM

    will object repository only takes xpath or any attribuite for facebook i have tried with id it shows wrong

    Reply
    • Mukesh Otwani says

      February 2, 2017 at 2:52 PM

      it can take any key value pair

      Reply
  8. Kumar says

    January 27, 2017 at 10:18 AM

    Hi Mukesh.
    I have mentioned a xpath value in properties file as mentioned below:
    xpathVal1=//*[@id=’aaa’]/option[1]
    if i call this in script like driver.findElement(By.xpath(pro.getProperty(“xpathVal1”).click
    it is working fine.
    Actually, i wanna use this in a loop. Xpath value is changing as below
    //*[@id=’aaa’]/option[1]
    //*[@id=’aaa’]/option[2]
    //*[@id=’aaa’]/option[3]
    So i wanna place this in a for loop.. how to do it.

    Could u pls help me out.

    I tried like this. I have placed the value in properties file as //*[@id=’aaa’]/option[“+i+”]
    and call this in for loop with integer value i from i =1; i<=200; i++..

    Reply
    • Mukesh Otwani says

      January 27, 2017 at 4:08 PM

      Hi Kumar,

      Whatever java reads from properties file is based on key-value pair. In your case, you are reading xpathVal1 as key then you will get value as //*[@id=’aaa’]/option[1]. Value is always taken as String datatype. Similarly xpath inside properties file like //*[@id=’aaa’]/option[“+i+”] never works. You need to process xpath separately in order to call these with index. Or mention all these xpaths in properties separately as xpathVal1, xpathVal2 & xpathVal3 and use them as per your requirement.

      Reply
      • Kumar says

        January 30, 2017 at 1:53 PM

        Thanks Mukesh. Will try that.
        Eventhough this question is not related to this post, i would like to ask you for basic clarification.
        I have used Selenium Implicit Wait in the starting of my script after the driver object initiated. driver.manage.timeouts().implicitlyWait(10, TimeUnit.SECONDS).
        So throughout my script for each element, will my script waits for 10 Seconds? or do i need to add the same line of code everywhere where i need to put wait?
        Also let me know if element on the page loads in 2 seconds what will happen? still it waits for 10 seconds?

        Reply
        • Mukesh Otwani says

          January 31, 2017 at 1:33 PM

          Hi Kumar kindly check http://learn-automation.com/explicit-wait-in-selenium-webdriver/

          Reply
  9. Daisy G says

    January 21, 2017 at 6:49 PM

    Hi Mukesh. Nice tutorial. Is there a way to find if a property in object repository properties file is id or name or tag name?

    Reply
    • Mukesh Otwani says

      January 21, 2017 at 8:42 PM

      Hi Daisy,

      Properties file works on key value pair values. Only demarcation between key & value is equals(=) symbol. Generally there is no way to provide value specific to id or name etc.

      Reply
  10. sanjay says

    December 15, 2016 at 8:34 AM

    u rcode above is not executing it is showing as in file[ut stream error.cannot load source file i.e obj repositry file

    Reply
    • Mukesh Otwani says

      December 15, 2016 at 7:06 PM

      Hi Sanjay,

      It seems file path is not correct. Please check file path.

      Reply
      • sanjay says

        December 18, 2016 at 6:31 AM

        thank you mukesh…u r awsome man….Am follwing your youtube channel too.your teaching skills are awsome..thank you so much…
        can you explain how to use inheritance concept in selenium for testcases….
        thank you

        Reply
        • Mukesh Otwani says

          January 11, 2017 at 3:19 PM

          Hi Sanjay,

          This is how we can use inheritance in Selenium https://www.youtube.com/watch?v=nJfGMjYjD58

          Reply
  11. Mayur says

    November 28, 2016 at 5:52 PM

    Hi Mukesh,
    Can you tell me how to create java files, each for test cases and test data?

    Reply
    • Mukesh Otwani says

      November 28, 2016 at 11:21 PM

      Hi Mayur,

      Your question is not clear. Can you repeat your question in simple manner?

      Reply
  12. Aparna says

    November 3, 2016 at 8:24 PM

    Went through some sites to understand property.I was confused. Here you have explained in very simple and clear manner.Thank you

    Reply
    • Mukesh Otwani says

      November 4, 2016 at 12:59 AM

      Most welcome Aparna 🙂 Keep visiting and let me know if any help.

      Reply
  13. Divya says

    August 21, 2016 at 10:41 PM

    Please email me the answers. Joined the blog. Thanks a lot

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:24 PM

      Sent

      Reply
  14. deepak sharma says

    August 15, 2016 at 12:54 PM

    Hi Mukesh,

    Request you to help me with Selenium framework, specially Hybrid and key driven framework.
    Where i can get your tutorial for these two frame work??

    Please let me know the path for your these two Hybrid and key driven framework tutorial.

    Thank you Mukesh

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 9:48 AM

      Hi Deepak,

      Here is the link learn-automation.usefedora.com/courses/selenium-frameworks-and-selenium-question-answers

      Reply
  15. Kalpit Prasad Karna says

    August 10, 2016 at 6:41 PM

    Hiii Mukesh, can you tell me how to write a list in page object repository?

    Reply
    • Mukesh Otwani says

      August 11, 2016 at 12:47 AM

      Hi kalpit,

      Can you explain in brief? question is not clear.

      Reply
      • Kalpit Prasad Karna says

        August 11, 2016 at 9:54 AM

        hiii mukeesh,
        how to write a list in a page object model using find by or find bys annotation.

        Reply
        • Mukesh Otwani says

          August 18, 2016 at 9:59 AM

          Hi kalpit,

          You can use By class to handle this.

          Reply
  16. kalyan says

    April 13, 2016 at 3:50 PM

    Hai Mukesh ,
    Your website is very useful for me thanks for your help & time. iam getting file not found exception how can i overcome it, did you posted any video tutorial on this concept please let me know.

    Reply
    • Mukesh Otwani says

      April 13, 2016 at 10:48 PM

      Hi Kalyan,

      File path is not correct please make the changes and let me know.

      Reply
  17. Sooraj Hat says

    February 24, 2016 at 7:58 AM

    Hello Mukesh, I have been using .properties files in my projects only because it was relatively easier to implement than Page Object Model. Can you please tell me between .properties object repository file and POM approach which one is more advantageous and standard?

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 2:19 PM

      Page Object model is more useful.

      Reply
  18. Manjunath says

    December 14, 2015 at 3:12 PM

    Hi Mukesh,
    Can you make a video on Selenium and TestNG Interview questions for 2+ experienced which will be helpful to us?

    Reply
    • Mukesh Otwani says

      December 14, 2015 at 10:09 PM

      Hi Manjunath hope you have gone through http://learn-automation.com/selenium-interview-questions-and-answers/ and be ready with all question.

      Reply
      • venkat says

        August 30, 2016 at 11:42 AM

        Hi Mukesh,

        Your website is very useful for me thanks for your help & time. please share me link for Page Object Model with Page factory…

        Reply
        • Mukesh Otwani says

          September 8, 2016 at 1:20 AM

          Hi Venkat here you go http://learn-automation.com/page-object-model-in-selenium-webdriver/

          Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?