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 Answer(s)

There are multiple ways to do this.

1. To every class pass the webdriver instance. As webdriver is a reference type you will just copy the address and your webdriver will be an updated version throughout.
2. Isolate the browser creation logic from Tests. Create a seperate class, lets say, BrowserFactory and in that class create a WebDriver object.  Call this class every time you want a webdriver instance. This mechanism when implemented properly is called a Factory design pattern,  Just take a look at factory pattern theory and create a class for that.

Sample code will look something like this, you may want to improvise on this as per your needs


package Factories;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;
/*

* This class has following responsibilities

* - To create required WebDriver

* - To maintain an instance of a WebDriver

*/

public class BrowserFactory {
private static WebDriver driver;
public static WebDriver getBrowser(String browserName) {

if (driver == null) {

if (browserName.equals("Firefox")) {
driver = new FirefoxDriver();

} else if (browserName.equals("InternetExplorer")) {

driver = new InternetExplorerDriver();

} else if (browserName.equals("Chrome")) {

System.setProperty("Webdriver.ie.driver", DataProviderFactory

.getRunConfigDataProvider().getChromeServerPath());

driver = new ChromeDriver();

}
long pageLoadTimeOut = DataProviderFactory

.getRunConfigDataProvider().getPageLoadTimeout();

long implictWait = DataProviderFactory.getRunConfigDataProvider()

.getImplicitWait();

driver.manage().timeouts()

.pageLoadTimeout(pageLoadTimeOut, TimeUnit.MILLISECONDS);

driver.manage().timeouts()

.implicitlyWait(implictWait, TimeUnit.MILLISECONDS);

}

return driver;

}
public static WebDriver getBrowser() {

return driver;

}
public static void closeWebDriver() {

driver.close();

driver.quit();

driver = null;

}
}

Virender

Virender Singh Professor Answered on June 2, 2015.
Add Comment

Hi Virender ,

Thanks for reply !!.

I’m aware of above pattern , but want to know how to make best use of it .

Form your answer i have few questions to make it more understandable .

1. DataProviderFactory is class which is written by you , i’mean some custom class not Selenium , right ?

2. Here if i consider “BrowserFactory ” as parent or base class , How to make use of bowser instance in other classes , Ex: From your method in Common reusable ,  Actual Test method in Module class(Module here mean which is project specific) ?

jagadeeshj01 Train Answered on June 3, 2015.
Add Comment

VDataProviderFactory is a custom class that I have written.
BrowserFactory has a static methods. You dont have to create instance of this class anywhere. All you have to do is
BrowserFactory.GetBrowser() from other classes and it will return you a webdriver instance.

Note: This class was designed by me for my framework, you will have to improvise or remove unnecessarily part based on your needs.

Virender

Virender Singh Professor Answered on June 3, 2015.
Add Comment

Virender ,

Do you really think its wise idea to call instance as “BrowserFactory.GetBrowser()” in long run i’mean in framework level , where your application might quite vast . I have another idea but need you help here in implementing it . Here it is

Create a base class for driver instance , then passing this instance as parameter and then in sub class methods where ever its required call  this instance as by Super(river).. this is one of implementation i had seen . Need your input in implementing it and working flow .

jagadeeshj01 Train Answered on June 4, 2015.
Add Comment

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?

Q2:
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)

{

super(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.

Virender

Virender Singh Professor Answered on June 4, 2015.
Add Comment

Your Answer

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