Page factory object - Question

31 views
Skip to first unread message

Siva D

unread,
Feb 21, 2021, 8:53:07 PM2/21/21
to seleniu...@googlegroups.com
Hello all,

I was learning page factory object concept and came across this code snippet in multiple forums and I am not clear on one of the lines. Please let me know your thoughts on the same or links that can answer this question. My question is around the second line in the constructor - "this.driver = driver;"  and I am not sure about the purpose of that line of code and almost all the web sources have something similar. 
My understanding is that Static method initElements creates a proxy for the page objects with the driver instance that is being passed as a parameter to this constructor and we use the page object to work on the @findby elements. I am not sure why this.driver =driver is required.

I am not sure what I am missing. Please let me know.

public PageObject(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

joseph...@gmail.com

unread,
Feb 22, 2021, 1:41:11 PM2/22/21
to Selenium Users
The code you've shown is an object constructor method (the object is called PageObject in this case).  The job of a constructor is to set up a new object when someone says

PageObject po = new PageObject(driver);

Not shown in your picture is the rest of the PageObject class.  Somewhere, probably near the top of that class, is a line something like this:

private WebDriver driver;

The constructor is doing two things to prepare the class to be used, but only the second thing is specific to page factory.

1) Set the variable driver (the one declared as 'private Webdriver driver') equal to the driver that was passed to the constructor method as a parameter.  This is not being used anywhere else *in the constructor method*, but it does make the driver variable valid for use in other methods in the PageObject class.  Specifically, when an object has a variable with the same name as a constructor parameter (like driver), this.driver refers to the variable in the class, and just plain driver refers to the parameter that was passed to the constructor method.

2) Call the PageFactory.initElements method using the passed in driver object.

Hope this helps.

Siva D

unread,
Feb 22, 2021, 2:09:39 PM2/22/21
to seleniu...@googlegroups.com
Thank you so much. From your explanation, I understood that it has nothing to do with page factory method and a design aspect for page objects to use the driver instance passed .

Below are the 2 sites that had similar style and got me thinking if it is required to be declared in the constructor that uses the page factory method. I was able to test without having the line of code in discussion but wanted to make sure that I have understood it correctly.








--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1247bafa-f9dc-4cce-ab6c-422c3817782an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages