Wednesday 5 February 2014

Creating Object Repository in Selenium WebDriver

1.      What is Object Repository?

Object Repository is a place where we can store objects information, it acts as interface between Test script and application in order to identify the objects during the execution

2.      Uses of Object Repository

1.      Object repository is a centralized location of the objects and their properties, so that if any object information changed in AUT, you need not to change in all the scripts, it is enough to change in the Object repository
2.      Easily maintain your tests or components when an object in your application changes

3.      Creating Object repository:

It’s up to you create an object repository, and which file you want to use, you can use properties file, you can use XML file….etc. You can learn about this in framework design concepts during the training; you can find the source code to read the values from the object repository in the project provided during the training.
Let us discuss about few ways here to create a object repository: 

First way to Create OR File:


1.      After create a Project, Create a following project structure in your project(I am Assuming here my application is Yahoo, and I am Following POM framework)



                                
  Note: Here Sagara is my client Name
2.      Create a simple text file with the Extension as .OR  (ex: LoginPage.OR) under object Repository Package (here it is com.sagara.objectRepository)
3.      Create a Simple java class file under test Pages package (ex : LoginPage.Java)
After above steps, your project looks a like:

4.      Open your LoginPage.OR and write the objects informations like this :



5.      Open your LoginPage.Java, and write the below code to get the objects information's from the OR Page and use it

          

Second way to Create OR File:


1.       After Creating a project, make sure that you will be having the following project structure (It is optional)



2.       Create a simple Java Class under testObjects Package(Let us say YahooLoginPage.Java)
3.       Create a simple java class under testscripts Package  (Let us say ‘TestYahooLoginPage.java’)

       

         
4.       Open your YahooLoginPage.Java and write the below code :

                
5.      Open TestYahooLoginPage. Java and write the below code :



Congrats!!...Now you have learnt how to maintain Object Repository in selenium

8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Suppose i want to automate Google auto complete drop down text , which show during search
    to do this we need to write some code like
    java.util.Listlist_suggestion = driver.findElements(By.xpath("//*[@id='gsr']/table/tbody/tr/td[2]/table/tbody/tr['x']/td"));

    if we use object repository how can we adjust our xpath where we need to pass a variable withing loop

    java.util.Listlist_suggestion = driver.findElements(By.xpath("//*[@id='gsr']/table/tbody/tr/td[2]/table/tbody/tr['x']/td"));

    int count = list_suggestion.size();

    System.out.println("count: " + count );

    for(int k=1;k<=count;k++)
    {

    String s = driver.findElement(By.xpath("//*[@id='gsr']/table/tbody/tr/td[2]/table/tbody/tr["+k+"]/td")).getText();

    System.out.println("Suggestion in row: " + k + " is : " + s );
    }

    ReplyDelete
  3. Hi Swarnendu,Thanks for your comment,Of course,agree with you,It is a basic idea to maintain objects information at centralized place,it has certain limitations,

    my idea behind this is to store the common objects information,for your case it will look like:


    ex: GooglePage.DropDownAutoComplete.xpath=//*[@id='gsr']


    java.util.Listlist_suggestion = driver.findElements(By.xpath(GoogleOR.GetProperty("GooglePage.DropDownAutoComplete.xpath"+"/table/tbody/tr/td[2]/table/tbody/tr['x']/td"));

    int count = list_suggestion.size();

    System.out.println("count: " + count );

    for(int k=1;k<=count;k++)
    {

    String s = driver.findElement(By.xpath(GoogleOR.GetProperty("GooglePage.DropDownAutoComplete.xpath")+"/table/tbody/tr/td[2]/table/tbody/tr["+k+"]/td")).getText();

    System.out.println("Suggestion in row: " + k + " is : " + s );
    }



    Benefit:if there is a change in table name,you no need to modify all lines of your code,in reality,it might be used in 100 of scripts,so we can save some time,

    Drawback :Index part we cannot use in object repository files

    Thanks
    Rajendra

    ReplyDelete
  4. Hi Rajendra

    Thank you for your reply and suggestion.

    Can you please share your knowledge on how to prepare a hybrid framework for selenium webdriver.

    i searched on web regsrding this but didnt got any good article with example. if you share this knowledge like you explained object repository it will be a great help.

    ReplyDelete
  5. Hi ,

    I have a question , If I have a table which rows & column count is dynamically changing every time then how i can know the count of rows & column.

    Let me know.

    ReplyDelete
    Replies
    1. You need to have a "rowcount" keyword in ur script which will get you the number of rows at runtime. Similarly "columncount". I had done it using Selenium RC. if you want you can make your script "Selenium Backward Compatible" and use these built in functions.

      Delete
  6. Kanhaiya, I thought of updating a separate post to work with Web Tables,But i am hardly finding time.meanwhile you can refer this example to work with dynamic web tables


    package testscripts;

    import java.util.List;
    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;

    /**
    *
    * @author Rajendra chary
    *
    */

    public class WorkingWithWebTables {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://dev.mysql.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testWebTable() throws Exception {

    driver.get(baseUrl + "doc/index-enterprise.html");

    //Identifying the webtable
    WebElement table=driver.findElement(By.xpath("//table[@class='docs-document-list']"));
    // WebElement table =driver.findElement(By.className("docs-document-list"));


    //Finding out the row collection
    List rowCollection=table.findElements(By.xpath("//table[@class='docs-document-list']/tbody/tr"));
    //Finding out the row count
    int rowcount=rowCollection.size();

    System.out.println("Number of rows in this table: "+rowCollection.size());

    int i_RowNum=1;
    for(WebElement rowElement:rowCollection)
    {
    List colCollection=rowElement.findElements(By.xpath("td"));
    int i_ColNum=1;
    for(WebElement colElement:colCollection)
    {
    System.out.println("Row "+i_RowNum+" Column "+i_ColNum+" Data ="+colElement.getText());
    i_ColNum=i_ColNum+1;
    }
    i_RowNum=i_RowNum+1;
    }
    }

    @After
    public void tearDown() throws Exception {
    //driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
    fail(verificationErrorString);
    }
    }

    private boolean isElementPresent(By by) {
    try {
    driver.findElement(by);
    return true;
    } catch (NoSuchElementException e) {
    return false;
    }
    }
    }

    ReplyDelete