Saturday, 31 May 2014

How to create Object repository in Selenium and How to use in Selenium Webdriver

One of the interview questions asked 

Step 1- Open note pad

Step 2- Put key and value pairs for example

facebook.login.username.xpath=.//*[@id='email']
facebook.login.password.xpath=.//*[@id='pass']
facebook.login.Signup.xpath=.//*[@id='loginbutton']

Step 3- Save file as .properties file (I saved in project home diretory and inside Object_Repo folder and name of object repository is facebook.properties

Step 4- Write Selenium script and use the same in script

Lets implement the same

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{


File src=new File("./Object_Repo/facebook.properties");

FileInputStream fis=new FileInputStream(src);

Properties pro=new Properties();

pro.load(fis);

System.out.println("Property class loaded");

WebDriver driver=new FirefoxDriver();

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

driver.get("http://www.facebook.com");

driver.findElement(By.xpath(pro.getProperty("facebook.login.username.xpath"))).sendKeys("Selenium@gmail.com");

driver.findElement(By.xpath(pro.getProperty("facebook.login.password.xpath"))).sendKeys("adsadasdas");

driver.findElement(By.xpath(pro.getProperty("facebook.login.Signup.xpath"))).click();


}



}

No comments:

Post a Comment