Automation Using Selenium Webdriver
Showing posts with label How to Create Object Repository in Selenium Webdriver- Selenium. Show all posts
Showing posts with label How to Create Object Repository in Selenium Webdriver- Selenium. Show all posts

Thursday 6 October 2016

How to Create Object Repository in Selenium Webdriver- Selenium

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

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 thatyou 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- 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




Select folder and specify name & extension


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 loginbutton for facebook loginpage.
Key you can write based on your project standard, but value should be correct which is coming from application
<span style="font-size: 14pt; font-family: arial, helvetica, sans-serif;">
facebook.login.username.xpath=.//*[@id='email']
facebook.login.password.xpath=.//*[@id='pass']
facebook.login.Signup.xpath=.//*[@id='loginbutton']</span>



4- Write Selenium script and use the same in script

Object Repository in Selenium Webdriver- Selenium Tutorial

package ObjectRepoExample;
<span style="font-size: 14pt; font-family: arial, helvetica, sans-serif;">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(); 
}}
</span>