How to pass the WebDriver instance to other Class in Selenium/Java/TestNG?

I have Class A and Class B. Class A has the driver initialization and opening the IE browser. I am able to run the test for all the @Test define in Class A which is working fine as expected but I am not able to execute @Test of Class B as the driver instance become NULL.

I tried to use @BeforeSuite and @BeforeTest but it’s not solving my purpose as the driver instance is valid only in Class A.

Can you please suggest me the solution so that I can initialize the driver only once to use for all the remaining Class rather then only for the Class it’s initialize?

I don’t want to create object of Class A and pass it to Class B to access the method.

Thanks in advance for your input.

biresh126 Default Asked on March 24, 2015 in Selenium WebDriver.
Add Comment
9 Answer(s)

Class A and Class B are test classes. Test classes ideally dont pass information between each other. For passing information either use a DataProvider, which woill be a static method in some class, or use a factory pattern.

Try to search for both and if you are not able to fix this, let me know. I will post some code.

Virender

Virender Singh Professor Answered on March 24, 2015.
Add Comment

Hello Virender,

I think Biresh is worried about getting same driver instance from parent class to all the other classes. As per my understanding, his intention is not with Dataproviders.  I also face the same issue before and got 2 solutions for it but I do not feel them as very best solutions. It would be great if you give some code for us. Below are the 2 ways which I identified.

1. Making driver as a static
2. Creating a constructor in other clases and passsing the driver instance to respective methods (which will be useful when you are returning page factory in Page objects).

Please let me know for any issues.

Many Thanks
Pavan

pavangurram Train Answered on March 24, 2015.
Add Comment

Pavan and Biresh

Pavan and Biresh,

I hope you have read about factory pattern design techniques. A class implementing factory pattern has a purpose to kepp an instance of desired class.

Let me show you a basic factory pattern class


public class BrowserFactory {
private WebDriver driver = null;

public static WebDriver getBrowser(String browserType)

{
if(driver == null)

{

if(browserType.equals("Firefox"))

{

driver = new FirefoxDriver();

}

else if(browserType.equals("Chrome"))

{

driver = new ChromeDriver();

}

else if(browserType.equals("IE"))

{

driver = new InternetExplorerDriver();

}

}
return driver;

}
}

try to go through the above code, you will see that one instance of WebDriver is maintained.

Part 2: Now this class can become the basis to your data provider, all we need here is to put a DataProvider annotation and get another way of finding what browser instance to create.

try it and let me know.

Virender

Virender Singh Professor Answered on March 25, 2015.
Add Comment

I created Class C and kept opening of browser and login into it where it’s initializing the static variable (WebDriver instance). Now this static variable is using (included in @BeforeClass) by Class A and Class B . It solved my problem.
Thanks a lot for all your valuable inputs and giving the clue to go for separate browser class. Kudos to all of you for sharing your knowledge!

biresh126 Default Answered on March 25, 2015.

Hi
Could you plz provide code here
I am struggling with same problem & not yet got the solution

Thanks

on November 2, 2015.
Add Comment

Hello Biresh,

Good to hear to got the solution. But it’s not a best practice to use static variable all the times. You may face issues while running parallel tests,  static variables and methods should be called only in static methods and in many cases. I followed 2nd approach in my above answer, also it is not very best solution for me so I’m trying other ways for it. I haven’t tried the way given by Virender yet. I will have a look and give you more inputs. Meanwhile if you get any other way to handle this please let me know.

Many Thanks
Pavan

pavangurram Train Answered on March 25, 2015.
Add Comment

Hello Biresh,

It seems we can use static driver to make it available in all instances / classes. I thought there would be a problem while doing parallel testing but it can be handle by using thread concept. I still have a concern by using static is you cannot call it in non static methods.

Virender — Can you please help us?

Many Thanks
Pavan

pavangurram Train Answered on March 25, 2015.
Add Comment

Basically there is no limitation of having a static variable or static method to get your webdriver.  You can class those variable or method from anywhere. Try the factory pattern class that I have suggested earlier. However in cases you run tests in parallel you cannot use the same webdriver across all tests even if you have thread synchronized your test. These are the reasons why I say that
– If same driver is being used by mutilple threads your tests will end up waiting on the webdriver object while the other one uses it. It will essential make you tests equal to being synchronized.
– You will also get exceptions because one test will put your driver some state which will be invalid for other test.

For parallel execution you always have to create a new driver per test. I have seen blogs and people talking about sharing a webdriver across truly parallel tests, but I wasn’t able to achieve it. In the comment let me post a sample two step tests which run in parallel, try it for your self.

Virender Singh Professor Answered on March 26, 2015.

Please look at the project shared here, you can run these tests in parallel, in synchronous fashion. Run it and let me know if you have any questions or if it doesnt solve your purpose

http://1drv.ms/1Nj6UGx

Virender

on March 26, 2015.
Add Comment

Virender,

Could you report the project shared?  OneDrive says link no longer works.

longelier Default Answered on June 19, 2016.
Add Comment

2nd way suggested by Pavan to initialize driver in Main class and create parameterized constructor in all test class is working absolutely fine. :)

Shaveek Default Answered on November 25, 2016.
Add Comment

Your Answer

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