Currently I have to create WebDriver instance in every class in @Before
methods. I want to know how can I create WebDriver only once and use it in different tests (<test>
tag in testng.xml
file).
Asked
Active
Viewed 581 times
1

Ahmed Ashour
- 5,179
- 10
- 35
- 56

Ajay Bhalekar
- 11
- 1
-
If you want to have multiple tests running parallely you cannot use single instance of webdriver efficiently. If you are ok with sequential execution you can use the below answer. – Shamik Jul 13 '15 at 10:21
2 Answers
0
There are many ways in which you can accomplish this .One way is to Create a common driver class
public class Driver {
public static WebDriver driver=null;
public static WebDriver startdriver(String browser){
if(browser.equalsIgnoreCase("Chrome")){
System.setProperty("webdriver.chrome.driver", "/home/vicky/Documents/Jars/chromedriver");
driver=new ChromeDriver();
}else if(browser.equals("Firefox")){
driver=new FirefoxDriver();
}
return driver;
}
}
In your before suite method initialize your driver
@BeforeSuite
public static void Openbrowser() {
Driver.startdriver("Firefox");
Driver.driver.manage().window().maximize();
}
//use the driver instance
@Test
public void goto(){
//Accessing the driver static variable by using classname.variable(Driver.driver)
Driver.driver.get("http://www.google.com")
}
Hope this helps you...kindly get back if you need any further help

Vicky
- 2,999
- 2
- 21
- 36
-
Static would cause same instance to be within all test tags. BeforeSuite would cause the same driver instance to be for all test tags. If he has multiple test tags, none of the above would work. – niharika_neo Jul 13 '15 at 08:34
-
@niharika_neo Thanks for the downvote.For your information this will work perfectly for multiple test tags because we initialize the driver class in our before suite method and then use the same driver instance for all the test methods I have used this approach in some of my projects it works perfectly fine – Vicky Jul 13 '15 at 09:20
-
@Vikcky Your code wont work when parallel execution of tests will be true, With multiple tests in testng.xml. It has to be an instance variable. Niharika is right. – Shamik Jul 13 '15 at 10:20
-
@Shamik I agree this is not a feasible solution for parallel tests but in his question the questionnaire has not mentioned anything about parallel execution and moreover niharika has mentioned in her comment that it will not work for multiple test tags but it will work for multiple test tags – Vicky Jul 13 '15 at 10:39
-
I think she was talking about parallel tests. Otherwise your code should work. – Shamik Jul 13 '15 at 10:51
-
@Vicky beforesuite would initialize for all test tags just once..so this won't work. There is a difference between test methods and test tags. Read more @ http://stackoverflow.com/questions/15218863/what-is-the-difference-between-test-method-and-test-tag-intestng/15233008#15233008 – niharika_neo Jul 18 '15 at 02:46
-
The questionairre has mentioned test tags in xml and not test methods and that is why the solution won't work. – niharika_neo Jul 18 '15 at 02:48
-
@niharika_neo Thanks for the reply...I know the difference between test tags and test methods The above solution works for multiple test tags also as i have mentioned that in the above comment.Before suite will initialize the driver which will be used in all test tags – Vicky Jul 20 '15 at 08:45
-
I still differ. Please read up the documentation. Before suite would init for all test tags just once. Try to write a simple code with invocation statements. Ideally for multiple test tags, for the questionnaire the driver init should happen multiple times. This won't happen with your code. – niharika_neo Jul 20 '15 at 13:11
-
Hi @niharika_neo The questionnaire has asked for a solution "create WebDriver only once and use it in different tests
tag".The above code satisfies that condition.Anyways ,the solution provided by you is better than mine. – Vicky Jul 21 '15 at 08:26 -
1Ok, I guess it is difference of interpretation then :) Only the questionaire knows now what he wants :) – niharika_neo Jul 21 '15 at 09:56
0
You can have a threadlocal webdriver instance instantiated in ITestListener's onStart method, which you can use in your test methods. Quit in onFinish method. This will share the driver instance between all your methods in all your classes in the test tag and create new instances of driver for all your test tags.
Make sure your parallel is either set to tests or false.

niharika_neo
- 8,441
- 1
- 19
- 31