RE: How to pass single browser instance to different classes

How to pass single browser instance to different classes ,
Say Ex : I have Class A where i initialize browser and steps of test will be spread across different method defined in different class say Class B , Class C . But my Actual test method is written in Class D .

Here how can i use single instance , Because i can see null value in driver in subClasses .
Please do let me know if more Info required .

Add Comment
5 Answers

Hey Jagdeesh,
Nice idea that you have shared. There are two questions here
1. Is having a factory managing driver instance a good idea?
2. Can we have a base test class and let the base test class contain a driver instance.

Q1: To answer this I will give you reference of Log4J. If you have used it, you will see that programmers always do this at the start of the class

Logger logger = LogManager.GetLogger(<ClassName>);

you will see this LogManager.GetLogger in every class of a project which uses a Log4j implementation. LogManager is a factory which manages and provides logger to every one. And Log4j is a corporate level tool which is used by very large projects. So having a factory pattern will not have any impact on a small or a large project. I dont know what exactly you thing the problem would be?

You can always have base test class and from the problem that you are explaining I see that you will have a similar BaseTest class structure

package SampleTest;
import org.openqa.selenium.WebDriver;
public class BaseTest {
protected WebDriver driver;
public BaseTest(WebDriver driver)


this.driver = driver;


and different test classes will look something like this

package SampleTest;
import org.openqa.selenium.WebDriver;
public class TestClass extends BaseTest {
public TestClass(WebDriver driver)


//Different test methods on the class go here!!!


Well I dont think this solves your problem. The reason why I am saying this is because, I am not sure from where will you get the driver instance? as you have mentioned you want to share the same driver instance to different test classes, I am not sure which class will maintain the driver instance?

Do let me know of these questions and I will be able to understand your problem better.


Professor Answered on June 4, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.