Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This has been asked a million times on the web, but there's no clear cut answer. I tried "-browserSessionReuse" today without any luck; my tests keep spawning new browser windows.

X:\QA\Automation\SELENIUM_SERVER>java -jar selenium-server-standalone-2.8.0.jar -log "log.txt" -browserSessionReuse -forcedBrowserMode "*googlechrome"
13:42:25.923 INFO - Will recycle browser sessions when possible.

Maybe it's how I'm starting Selenium in my .py files?

sel = selenium('localhost', 4444, "*chrome", 'http://server/')

...perhaps I'm doing too many server starts?


share|improve this question
I don't think that's possible. afaik Selenium always uses new Window – Tarun Oct 19 '11 at 3:21
I've had the same result, and I wanted reuse at one point to check cookie values in a site test. – MichaelF Oct 19 '11 at 12:51
up vote 13 down vote accepted

This is possible with Selenium 2 and WebDriver. I'm not sure if it is with different versions. In Selenium 2 with WebDriver you can call webDriver = new FirefoxDriver() which spawns a browser, and that browser will stay open for the duration of your testing, or you can choose to close it with webDriver.Quit().
I like to close my browser window between tests so that I know my tests aren't "dirty" with stored session data that could affect how the tests run, but I can see value in some targeted tests, where I want to try a couple different scenarios while keeping the same session going.

share|improve this answer
Funny, I was just reading about this this morning, and I think it will solve my browser windows problem. Thank you. – Aaron Oct 19 '11 at 18:19

I have done it in the past by declaring my Selenium driver instance as a static member loaded by the entire test suite. Then, pass that member as an argument to each test. Then, I also created some methods to keep track of how many windows are open and make sure that the last window is never closed. Then, after each test I close all windows except for the last one and redirect the last window to "about:blank". Then, as my tests run, it reuses the windows.

I have since then, abandoned this method because I no longer need it, for the following reasons:

  1. Using a static global webdriver object was bad programming practice. My driver was part of a Utility class extended by all tests and therefore my test classes could not extend any other class. You can get around this my using Scala and/or JDK1.8 because they have the "defender method" interface feature, but since I am using JDK1.7, I needed to do this the right way.
  2. Now I close the browser after each test but since I now use "Selenium Grid" I dont need to worry about the performance impact of re-opening browsers anymore because I can throw more hardware at the problem and run multi-threaded.

NOTE: Here is my example of how I do things now but keep in mind that there are now better ways of doing this.

share|improve this answer

You can achieve it by using same selenium instance in multiple test. That requires some code design that controls start and stop selenium. We are using ISFW that provides a way using java so that's for sure it is possible with some efforts in python as well.

share|improve this answer

This worked for me in

var webdriverio = require('webdriverjs-angular');
var options = {
    desiredCapabilities: {
        browserName: 'firefox'
    ngRoot: 'html' // main application selector

var wd = webdriverio.remote(options).init();

var wd2 = webdriverio.remote(options);
share|improve this answer

I faced the same issue using TestNG. I had used @BeforeMethod and for each method new browser is opened.

When I changed it to @BeforeTest instead of @BeforeMethod, that worked for me.

share|improve this answer
If you pass in the driver instance via test method argument (from the DataProvider, then you want to use BeforeMethod, but if you have a shared driver in the class for multiple test methods (which i think is a bad idea) then you want to use BeforeTest, of course. – djangofan Feb 1 at 2:20

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.