-3

When every test class will inherit driver, then why should we create constructor that takes driver object?

public class BaseTest{
 public static Webdriver driver = new ChromeDriver();
}

public class LoginPage{

 Webdriver driver;

public LoginPage(WebDriver driver){
this.driver = BaseTest.driver}

class LoginTest extends BaseTest{

Login login = new Login(driver);
}
  • 1
    I would review your architecture, you have created a circular dependency graph. – João Farias Jun 27 at 22:24
  • 1
    public LoginPage(WebDriver driver){ this.driver = BaseTest.driver} . In this constructor you are not even using the passed driver parameter . So it's doing anything much. It's getting the instance created in base class . – PDHide Jun 28 at 2:10
  • It's not doing anything – PDHide Jun 28 at 4:41
0

As PDHide pointed out, the Driver you are sending is not used (although you are sending the same instance as you had before.

However, the biggest problem is that you are creating a circular dependency graph. It means that any change in any class would require all of them to be re-compiled and re-deployed (and worse of all, means that any change can introduce bugs in any class).

enter image description here

Since you want to have a single instance of Driver, you can break this loop by introducing a Singleton. Additionally, you may want to add a Driver interface so you won't couple your page objects to Selenium (particularly for unit testing purposes).

enter image description here

This way, your tests can do something like to fetch the instance:

Login login = new Login(SeleniumDriver.getInstance());

And your page objects would simply:

public LoginPage(Driver driver){
   this.driver = driver
}

This way you can create test doubles for the Driver interface to check the page objects in isolation.

If you want to furthermore isolate your tests from the page objects themselves, you may want to add a Factory for the page objects.

| improve this answer | |

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