1
votes

I have gone through the many blogs and sites to get more information about POM (Page Object Model). However, some of blogs gives examples using @FindBy with PageFactory for getting the web element and some of blogs has create the method with written by WebElement and finding element using findElement() methods as given below:

 1. @FindBy(id="clickhere")
    WebElement linkClickHere;

     public classconstructor(WebDriver driver){
        return PageFactory.initElements(driver, classname.class);
     }

 2. public static WebElement lnk_MyAccount(WebDriver driver){
       return driver.findElement(By.id("clickhere"));
    }

I understand that, using factory we get all the element of web page before executing the script and from second point, it looks for element at the time of script execution, correct me if I am wrong.

Now, which one we should use? Which is the best among this? Is there any difference between this two? What are the advantage and disadvantage for this two?

I would appreciate your inputs....

1

1 Answers

2
votes

PageFactory is a clean coding solution provided by Selenium to support the Page Object Model. And no, it doesn't get all web elements before execution, as it delivers you transparent proxies only, not concrete instances of the web elements. So you will get a fresh copy of the actual instance when you first do something with the element, not any earlier. So in this respect, PageFactory doesn't have a disadvantage.

The only drawback of PageFactory compared to findElement() is that you cannot get the web driver via the WrappedDriver property of the web element, so you have to keep track of the driver instance yourself. But aside from that, PageFactory is a neat, clean way of implementing the POM and should be used whenever possible.