How to avoid empty extra browser opens when running parallel tests with TestNG
<p>While I'm running parallel test cases using TestNG and selenium-webdriver, there are some browser instances that open empty and doesn't close even after the execution finish. I have read several topics on different forums asking about similar problems but none of them seems to be the same issue. </p>

<p>Also seems like more empty browsers are open when I add more test to the suite. I'm not sure what of the following information will be relevant for this problem but I will try to provide a complete picture of what I have:</p>

<ul>
<li>I'm using (as mentioned) TestNG - webdriver.</li>
<li>I'm working with page objects pattern.</li>
<li>I'm running my automation from a javafx interface, which allow the user to select some options.</li>
<li>I'm using ChromeDriver to emulate mobile devices.</li>
<li>I'm using a local thread of the driver which allow me to use an exclusive static driver for each test.</li>
<li>No empty browser are open when I don't run in parallel.</li>
</ul>

<p>This is my TestNg.xml:</p>

<pre><code>&lt;suite name="Suite" parallel="classes" thread-count="4"&gt;
&lt;test name="Test"&gt;
&lt;groups&gt;
     &lt;run&gt;
        &lt;!-- Use include to specify what you want to run or exclude to not run the mentioned group(s) --&gt;
        &lt;include name="fullRegression"/&gt;
        &lt;exclude name="brokenOrDevelopment"/&gt;
     &lt;/run&gt;
  &lt;/groups&gt;
&lt;packages&gt;
    &lt;!-- Packages who contains the test cases classes --&gt;
    &lt;package name="Test"/&gt;       
&lt;/packages&gt;
</code></pre>

<p> 
 </p>

<p>This is the class which create the driver instance</p>

<pre><code>public class TestCaseConfiguration {
 protected HomePageObjects homePage = null;
 protected DesiredCapabilities  capabilities;
 protected ExtentReports REPORTMANAGER;
 protected ExtentTest REPORT;

 public static ThreadLocal&lt;ChromeDriver&gt; driver;

 @BeforeMethod(alwaysRun = true)
 public void setup() throws InterruptedException {

     System.setProperty("webdriver.chrome.driver", "src/Tools/chromedriver.exe");//chrome driver version 2.20 | We need to have a relative path since no all the user will have the driver on C: 

     capabilities = DesiredCapabilities.chrome();
     Map&lt;String, String&gt; mobileEmulation = new HashMap&lt;String, String&gt;();
     mobileEmulation.put("deviceName", Constants.DEVICE);
     Map&lt;String, Object&gt; chromeOptions = new HashMap&lt;String, Object&gt;();
     chromeOptions.put("mobileEmulation", mobileEmulation);

     capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

     /**
      * dirver is a ThreadLocal&lt;ChromeDriver&gt;. This allows the driver to be static and available for the complete project, and exclusive for each thread. 
      */
     driver=new ThreadLocal&lt;ChromeDriver&gt;()
                {
                    @Override
                    protected ChromeDriver initialValue()
                    {
                        return new ChromeDriver(capabilities); 
                    }
                };
     driver.get().manage().deleteAllCookies();
     driver.get().manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
     driver.get().navigate().to(Constants.DOMAIN);

     homePage = PageFactory.initElements(driver.get(), HomePageObjects.class);
 }

 @AfterMethod(alwaysRun = true)
 public void close() throws InterruptedException {           
    driver.get().close();
    driver.get().quit();  
 }
</code></pre>

<p>}</p>

<p>This class is extended by each test case class. The tests run as expected, the only problem seems to be the extra browser instances.</p>

<p>Doesn't anyone know how can I avoid that issue or if I'm doing something wrong?
Thanks for the help.</p>

<p><a href="http://i.stack.imgur.com/wNwrD.png" rel="nofollow">This is how the browser looks when open empty</a></p>