Advance Selenium

Complete Guide to Set 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

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

46 thoughts on “Complete Guide to Set Object Repository in Selenium Webdriver

  1. Yash says:

    Hi Mukesh,

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

    Regards,
    Yash

    1. 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

  2. Tanushree says:

    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

    1. 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));

  3. Tanushree says:

    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.

    1. Hi Tanushree,

      Are you getting NullPointerException in console?

  4. mani.bhute@gmail.com says:

    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?

    1. 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.

  5. Myra Khetpal says:

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

    1. Hi Myra,

      Check whether you are providing correct path for file.

      1. Myra Khetpal says:

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

  6. Radha says:

    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

    1. 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.

  7. siri says:

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

    1. it can take any key value pair

  8. Kumar says:

    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++..

    1. 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.

      1. Kumar says:

        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?

  9. Daisy G says:

    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?

    1. 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.

  10. sanjay says:

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

    1. Hi Sanjay,

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

      1. sanjay says:

        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

        1. Hi Sanjay,

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

  11. Mayur says:

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

    1. Hi Mayur,

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

  12. Aparna says:

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

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

  13. Divya says:

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

  14. 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

    1. Hi Deepak,

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

  15. Kalpit Prasad Karna says:

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

    1. Hi kalpit,

      Can you explain in brief? question is not clear.

      1. Kalpit Prasad Karna says:

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

        1. Hi kalpit,

          You can use By class to handle this.

  16. kalyan says:

    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.

    1. Hi Kalyan,

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

  17. Sooraj Hat says:

    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?

    1. Page Object model is more useful.

  18. Manjunath says:

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

      1. venkat says:

        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…

Leave a 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.