0

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);
1
  • 1
    Did 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 static
    – PDHide
    Commented May 17, 2020 at 8:09

3 Answers 3

2

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.

1

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.

-1

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:

  1. We only need a single shared WebDriver instance for the whole test. So, static Webdriver instance is shared and passed to all Page classes.
  2. 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.

4
  • There is no base class, or else it would read "LoginPage extends SomeBaseClass". Single instance is not the same as static. Commented 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
    Commented May 20, 2020 at 14:36
  • That is also an assumption, I for example never use a testbase class in my test. :) Commented 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
    Commented May 21, 2020 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.