Search code examples
instancepage-factory

Difference between pagefactory.initelements and Class instance


I am new to java.

  1. I am not able to get my understanding around the difference between Pagefactory.initelements and Class instance. Can someone please help me on this ? Reason for this question is :

The only difference I can see is the webelement initialisation and nothing apart from that. Both can be used to access the class variables and methods.

  1. Somename.class will do the same as 'new someclass()' ?

Solution

  • Page Factory uses Java Reflection API. It also has 2 public constructors for developers. One accepts already instantiated object as an argument. The second allows you to pass Object.class which is not an instance, it's more like a schema of a class.

    PageFactory.initElements(driver, this);
    

    The above method accepts the instance of Page Object, already created instance. Then, it gets it's a schema, reads fields and initializes them based on @FindBy annotations.

    PageFactory.initElements(driver, PageObject.class)
    

    The above class already has the schema so it initializes the fields and returns a newly created instance of PageObject class.