1

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

Learner
  • 15
  • 1
  • 7
  • Have a look on below threads: 1. http://stackoverflow.com/questions/17819905/selenium-difference-between-findby-and-webelement-findelement 2. http://stackoverflow.com/questions/21723730/when-do-findby-attributes-trigger-a-driver-findelement 3. http://stackoverflow.com/questions/17819905/selenium-difference-between-findby-and-webelement-findelement – Karim Narsindani Mar 14 '16 at 06:45

1 Answers1

2

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.

Kim Homann
  • 3,042
  • 1
  • 17
  • 20
  • is the better approach if I create the POM using 2 option? The thing is that I am passing the locator value in variable and for FindBy it's required constant variable which cannot be initialized in constructor of class as it has static key word... I have the value of locator stored in excel sheet so I update the change in excel sheet and script read it from excel sheet than it start execution – Learner Mar 14 '16 at 12:14
  • If you store your locators in an Excel sheet, then this is of course a totally different approach than PageFactory, where the locators are in the `FindBy` statement, which again requires a constant value. So your approach is absolutely Ok but doesn't go together with PageFactory. – Kim Homann Mar 15 '16 at 10:47
  • 1
    Maybe another thought about philosophy: POM means to me that everything page-related goes into one place, which is the class file of the page. By storing the locators in an Excel sheet, you somehow break this paradigma. I don't wanna say that this is a bad thing. It's just something a professional programmer should consider and be aware of. – Kim Homann Mar 15 '16 at 10:50
  • Many thanks for your inputs which has help me in understanding the POM and pagefactory in better way – Learner Mar 15 '16 at 10:59