-1
WebDriver driver = new ChromeDriver();

Now, when we "instantiate" Webdriver and create a reference variable and then assign the object returned to it, then does it mean that we have a new Chrome Browser session/tab open with "Default" ChromeOptions settings.

Does it mean, that when we pass the driver instance/object into a constructor or any other method in any child class, then its passing that same original object with default setting.

public class Login{
Webdriver driver;

public login(Webdriver driver)
{
this.driver = driver;
}

What is the purpose of passing a "driver" object to any method. Why not create a new Driver instance for each and every class in the project?

Thanks.

2

1 Answer 1

1

You create an instance of the ChromeDriver class. You pass this instance to your login class and put a reference to the instance into the this.driver. This so that you reuse the same browser for each of your page-classes. You need to store the reference somewhere so that when you later re-use the class it knows how to find the driver instance.

public class Login {
  Webdriver myLocalDriverReference;

  public Login(Webdriver driverInstance)
  {
    this.myLocalDriverReference = driverInstance;
    myLocalDriverReference.get("http://server/loginpage.html")
  }

Each time you create an instance of a WebDriver with the new keyword it launches a new fresh empty profile in a new browser. It does not open a new session or tab, it starts a new executable process.

So if you do not want to share state between test pages/steps and you are fine with the overhead of starting a browser you could create a new instance every-time.

Read:

3
  • 1
    +1 for a detailed answer. " It does not open a new session or tab, it starts a new executable process." - thanks for this, i can clearly understand now.
    – jerry67
    May 28, 2020 at 3:41
  • @jerry67 a new process will have a new session id
    – PDHide
    May 28, 2020 at 5:24
  • We interact with the browser using session id , create two instance a = chromedriver() , b=chromedriver () , you can see two browsers getting open . Add a sleep and Close both browser , and call a.get(url) b.get(url) u will see both are different session id , it throws session xx not found
    – PDHide
    May 28, 2020 at 5:29

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