I am a complete newbie in Selenium and java in general. I am trying to use the Page object model with Page factory to create Page object class for the Login page of an application. I am reading lot of sample code on the web tutorials and there the "Webdriver" reference is delared as "static". Is it that other classes can use the Base class driver instance?
package com.test.login;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class LoginPage {
**public static WebDriver driver = null;**
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initelements(driver, this);
}
@FindBy(xpath = "//input[contains(@id,'username')]")
public WebElement User_ID
@FindBy(xpath = "//input[contains(@id,'password')]")
public WebElement Password;
@FindBy(xpath = "//input[contains(@id,'login')]")
public WebElement Login_Button;
LoginTestClass.java
public class LoginPageTest{
WebDriver driver;
LoginPage loginpage = PageFactory.initElements(Login.class);
-
1Did you removed static and tried running the code ? Unless you are not directly accessing the class variable from another non subclass you don't need staticPDHide– PDHide2020年05月17日 08:09:31 +00:00Commented May 17, 2020 at 8:09
3 Answers 3
Basically you do not even need to store your driver reference in page class field unless you access some driver-specific features within the logic of your page class (like accessing page title, window management, etc.)
As to me the latter is better to take out (I consider using driver object within a page class methods as a sort of anti-pattern) of page class code so personally I do never have webdriver field in my page classes. Neither static, nor a regular one.
In general static fields is a sort of thing which I would not recommend to use unless you have a good understanding of language concept since they might increase the risks of everything will go wrong, especially in multi-threaded use-cases.
Using static will make the scope class level, else the scope will be the object level. You cannot share the state between classes
You can use base class driver instance by inheriting it or by calling Baseclass.driver then you need static keyword.
Would need to see the Driver declaration on the Test Setup Base class to understand this completely.
From my limited understanding, there are two reasons for this static usage:
- We only need a single shared WebDriver instance for the whole test. So, static Webdriver instance is shared and passed to all Page classes.
- Since a static object is passed to each of the page classes in the constructor, each page class need to declare it as static locally. But like @Alexey mentioned in his answer, doing actions on the driver within the page object class is not recommended. Those are better kept in Test Classes.
This also has the advantage that the same WebDriver instance is used throughout the session and therefore there is no need for explicit data transfer between objects.
-
There is no base class, or else it would read "LoginPage extends SomeBaseClass". Single instance is not the same as static.Niels van Reijmersdal– Niels van Reijmersdal2020年05月17日 20:45:12 +00:00Commented May 17, 2020 at 20:45
-
@NielsvanReijmersdal I think there was some confusion. I was talking about the Base class for the tests(where the driver setup is done), not the base class for Pages. OP had mentioned the Base class in his question and I was talking about that.Renju Jose– Renju Jose2020年05月20日 14:36:03 +00:00Commented May 20, 2020 at 14:36
-
That is also an assumption, I for example never use a testbase class in my test. :)Niels van Reijmersdal– Niels van Reijmersdal2020年05月20日 15:51:03 +00:00Commented May 20, 2020 at 15:51
-
@NielsvanReijmersdal I'd like to learn more. Do you have a sample repo I can take a look at?Renju Jose– Renju Jose2020年05月21日 19:21:11 +00:00Commented May 21, 2020 at 19:21
Explore related questions
See similar questions with these tags.