Object Repository in Selenium Using TestNG
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
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
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 in previous posts;Let us discuss about few ways here to create a object repository:
But this time we are going to use Object Repository to take the value as input.
How to create new properties file
To create new properties file, Right click on your project package -> New -> Other .
It will open New wizard. Expand General folder In New wizard and select File and click on Next button.
Give file name objects.properties on next window and click on Finish button.
It will add object.properties file under your package. Now copy paste bellow given lines in objects.properties file
objects.properties
username_testbox=Email
password_textbox=Passwd
Signup_btn=signIn
password_value=*******
In above file, Left side value Is key and right side value Is element locator(by name) i.e) (By.name("Email")
of all web elements of Gmail page. You can use other element locator methods too like xpath, css ect..
All the element locators are coming from objects.properties file using obj.getProperty(key). Here key Is reference of element locator value in objects.properties file.
Finally your project explorer look like this
Copy and paste blow code in package frameworktestng -> logintest.java
package framworktestng;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class logintest {
private WebDriver driver;
@BeforeClass
public void Startup() {
driver = new FirefoxDriver();
}
@Test(description = "Read Gmail account")
public void Login() throws Exception {
driver.get("http://www.gmail.com");
File src=new File("object.properties");
FileInputStream fis=new FileInputStream(src);
Properties obj=new Properties();
obj.load(fis);
System.out.println("Property class loaded");
driver.findElement(By.name(obj.getProperty("username_testbox"))).sendKeys(obj.getProperty("username_value"));
driver.findElement(By.name(obj.getProperty("password_textbox"))).sendKeys(obj.getProperty("password_value"));driver.findElement(By.name(obj.getProperty("Signup_btn"))).click();
Thread.sleep(9000);
}
@AfterClass
public void teardown() {
driver.quit();
}
}
No comments:
Post a Comment