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

Here locator means web element id, name, CSS, XPath,class name etc.

Object Repository in Selenium Webdriver

To understand an importance of Object repository, we will take 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 locator 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 so to overcome with this we will move all locator in a separate file and we will link all test cases to this file. In case any changes happen in our locator we will simply change in that file, not our test cases.

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

Object Repository in Selenium Webdriver- Selenium Tutorial

Precondition
1- Project should be created and Selenium jars should be added
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

Select folder and specify name & extension

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

object.properties:

Google_URL=http://www.google.com
Search_box=//input[@name='q']
Button=//button[@name='btnG']

Sample Code:

package Others;
import java.io.File;
import java.io.FileInputStream;
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.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class objectrepositry {

@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("D:\\WebDriver\\object.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 ChromeBrowser
System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");           
WebDriver driver = new ChromeDriver();    

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

// Pass application
driver.get(pro.getProperty("Google_URL"));

// find the text box and enter the text

driver.findElement(By.xpath(pro.getProperty("Search_box"))).sendKeys("Properties");

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

// click the search button

driver.findElement(By.xpath(pro.getProperty("Button"))).click();

driver.quit();

}

}

Note: Here Google_URL, Search_box and Button locator values mentioned in the object.properties file.

No comments:

Post a Comment