1.

What is Object Repository? How can we create an Object Repository in Selenium?

Answer»

Object Repository is a centralized location where you can STORE the object information. Object repository acts as an interface between Test script and application in identifying the objects during the execution.

The collection of web elements belonging to AUT that is Application Under Test ALONG with their locator VALUES is termed as Object Repository. During execution whenever an element is required in the script, locator value is populated from the object repository. Instead of hard coding locators in the script object repository can be used and locators can be stored in a central location. Objects are stored in an excel sheet or XML file which populates inside the script whenever required.

It is always recommended using an external file for object repository rather than hard coding the objects and its properties directly into our code and Reason is as it reduces the maintenance effort and provides a positive Return on investment. If any of the object properties change within the application under test, we can easily change it in external object repository file, instead of searching and doing updates for that object individually in the code.

Before creating an object repository pre-requisite is project is CREATED and Selenium jars are added.

1. Create a new file with extension .properties

For example, object_repo.properties

To create new file right click on project> New > File

2. Select the parent folder and specify file name & extension.

3. Now Double click on file> File will open in Edit mode

As object repository works on KEY and Value pair, you will specify Keys based on project and values we will give locator value.

Let us take XPath for username, password and login button for facebook login page.

  1. facebook.login.username.xpath=.//*[@id='email']
  2. facebook.login.password.xpath=.//*[@id='pass']
  3. facebook.login.Signup.xpath=.//*[@id='loginbutton']</span>
  4. Write Selenium script and use the same in the script
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 {  public void TestObjRepo() throws IOException{  // Specify the file location used. //we have object repository inside project directory only File File1=new File("object_repo.properties");  // Create  FileInputStream object FileInputStream fis=new FileInputStream(File1);  // Create Properties class object to read properties file Properties prop=new Properties();  // Load file so we can use into our script prop.load(File1);  System.out.println("Property class loaded");  // Open FirefoxBrowser WebDriver driver=new FirefoxDriver();  // Maximize window driver.manage().window().maximize(); // Launch application driver.get("http://www.facebook.com"); // Enter username use keys which is specified in Object repository. // Here getProperty is method which will accept key and will return value driver.findElement(By.xpath(pro.getProperty("facebook.login.username.xpath"))). sendKeys("testor@gmail.com"); // Enter password here used keys specified in Object repository. driver.findElement(By.xpath(pro.getProperty("facebook.login.password.xpath"))). sendKeys("password"); // Click on login button here use keys specified in Object repository. driver.findElement(By.xpath(pro.getProperty("facebook.login.Signup.xpath"))).click();  } } </span>


Discussion

No Comment Found