Object Repository Class Example

To overcome the drawbacks of the .properties file, I create different (appropriately named) classes for each page and create a collection of object and properties from that particular page. In that class I create a constructor and custom methods. Now I can call these methods/ classes as and when required. Further, when it comes to editing any object property, it is more convenient as compared to editing .properties files.

This is how to create OR class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class orExample {

 WebDriver driver;
 
 public static String signInButton = "//*[@data-g-label = 'Sign in']";
 public static String un = "//*[@id = 'Email']";
 public static String next = "//*[@name= 'signIn']";
 public static String pw = "//*[@type= 'password']";
  
// Now we will create a constructor to call this reusable class 
 public orExample(WebDriver driver)
 { 
  this.driver=driver;  
 }  
}


And this is how to use/ reuse the objects/ properties form the above Object Repository class:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Sample to call the reusable Object Repository class  
@Test
public void orLoginSample()
{
 orExample orLogin = new orExample(driver); 
 driver.findElement(By.xpath(orExample.signInButton)).click();
 driver.findElement(By.xpath(orExample.un)).clear();
 driver.findElement(By.xpath(orExample.un)).sendKeys("username");
 driver.findElement(By.xpath(orExample.next)).click();
 driver.findElement(By.xpath(orExample.pw)).clear();
 driver.findElement(By.xpath(orExample.pw)).sendKeys("password");
}


I find this method more crisp and compact and efficient than .properties File method, but Page Object method and Page Factory methods have further streamlined re-usability! 


No comments:

Post a Comment