3

I can create a BaseTest.java file and declare Public static WebDriver driver; in it. Then all classes can extend and use the same driver reference.

Then all my test classes can use same instance.

Can I use:

driver = new ChromeDriver(); 

in every testng method or Cucumber scenario. I'm confused, how to use driver. Is driver a chrome window session?

0

3 Answers 3

2

Is driver a chrome window session?

No. A ChromeDriver represents a server than controls a Chrome browser running locally. The server will control the lifecycle of the browser, such as initiation, killing and controlling actions.

For more details, see the docs

Can I create driver = new ChromeDriver(); in every testng method or Cucumber scenario

Yes, but it will depend on your context. If you want to have multiple browsers running independently in some actions, you can create a new driver.

Bonus:

I can create a BaseTest.java file and declare Public static WebDriver driver; in it. Then all classes can extend and use the same driver reference.

This may be dangerous. Inherantance is the strongest form of coupling you can make and having a global variable can cause change in behaviors very difficult to understand. You may want to refactor your code so that the Driver instances would be injected into your Page Objects. This way you are not coupling the Page Objects to a global variable and you can easily create unit tests for them by injecting a test double for the Driver.

2

What is chromedriver:

chromedriver has code that can interact with browser. THis commands are exposed through an API ( This is what you see as chromedriver started on localhost:14563 ) . From the code we access this api endpoints to tell chrome driver to do something.

Example, when you say elem.click() , selenium talks to the API and tells chromedriver to click the specified elem in the browser. And chrome driver does it and returns response from browser and give it back to out code

enter image description here

Why we need base class

This is to ensure driver sessions are passed among different classes. Else you won't be able to do something like

   page1.sendpassword()
   page1.sendusername()
   page1.submit()
   page2.adduser()

Because if you have different driver session for page1 and page2,

then once you start page2.adduser() chrome driver will be started newly and your previous login sessions will not be available so you have to login again

Also you cannot add screenshots or driver-specific actions in listener classes for reporting

You can read more about base class at :

What is the purpose of creating a BaseTest class which declares a global static WebDriver object in POM framework?

0

WebDriver instance in your code is a kind of channel that you use to interact with a single session of web driver implementation. Basically what you use in your code is so called client binding.

A WebDriver is an HTTP server that exposes REST API towards your client bindings (e.g. Java libraries) on one side and can control browser on other side.

So all your code that is assumed to work with the same chrome session (preserving cookies, etc.) has to share the same instance of web driver.

Nothing stops you from instantiating one more diver in your code. Through that new instance you will obtain the control of a different parallel isolated browser session.

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.