title: Parallel run of Selenium tests (uses ThreadLocal) results in orphaned browsers being opened
body: <p>I use ThreadLocal for thread safety and run the tests in parallel using Maven failsafe and Junit. I am running two tests from two feature files to test parallel running.
  But I always have the first browser blank. Then the subsequent ones are fine and the tests pass.
  If I run sequentially, there is no issue.</p>
  <p>HookStep class:-</p>
  <pre><code>         public class HookStep {
        
              @Before()
              public void beginTest() {
                  WebDriverFactory.setDriver(Props.getValue(&quot;browser.name&quot;));
              }
      
        @After()
          public void stopTest(Scenario scenario) {
              switch (environment) {
                  case &quot;local&quot;:
      
                  case &quot;aws&quot;: {
                      if (scenario.isFailed()) {
                          Screenshots.Shot shot = new Screenshots(Screenshots.CONTEXT_TEST_FAIL)
                                  .takeShot(scenario.getName() + formCounter.getAndIncrement() + &quot;&quot;);
                          scenario.embed(shot.getContent(), &quot;image/png&quot;, &quot;Error - &quot;);
                      }
                      WebDriverFactory.closeBrowser();
                  }
  
  }
  </code></pre>
  <p>WebDriverFactory class :-</p>
  <pre><code>public class WebDriverFactory {
  
      private static ThreadLocal&lt;WebDriver&gt; driver = new ThreadLocal&lt;&gt;();
  
      public static synchronized void setDriver(String browser) {
  
          switch (browser) {
              case &quot;chrome&quot;:
                  driver = ThreadLocal.withInitial(() -&gt; {
                      WebDriverManager.chromedriver().setup();
                      return new ChromeDriver(BrowserOptions.getChromeOptions());
                  });
                  prepareBrowser();
                  break;
              case &quot;fireFox&quot;:
                  driver = ThreadLocal.withInitial(() -&gt; {
                      WebDriverManager.firefoxdriver().setup();
                      return new FirefoxDriver(BrowserOptions.getFirefoxOptions());
                  });
                  break;
              default:
                  throw new IllegalStateException(&quot;Unexpected value: &quot; + browser);
          }
      }
      private static void prepareBrowser(){
          getDriver().manage().window().maximize();
          getDriver().manage().deleteAllCookies();
          getDriver().manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
          getDriver().manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
      }
      public static synchronized WebDriver getDriver(){
          return driver.get();
      }
      public static void closeBrowser() {
  
          getDriver().quit();
      }
  }
  </code></pre>
  <p>StepDef Class:-</p>
  <pre><code>public class SampleStepDef {
      private final WorldHelper helper;
  
      public SampleStepDef(WorldHelper helper) {
          this.helper = helper;
      }
  
    
      @Given(&quot;I click on the URL&quot;)
      public void iClickOnTheURL() {
     
         helper.getSamplePage().navigateToSite();
  
      }
  }
  
  
  public class WorldHelper {
      WebDriverFactory webDriverFactory=new WebDriverFactory();
  
      protected  WebDriver webDriver = webDriverFactory.getDriver();
      private  BasePage basePage;
      private  SamplePage samplePage;
  
      public SamplePage getSamplePage(){
          if(samplePage!=null) return samplePage;
          samplePage= PageFactory.initElements(webDriver,SamplePage.class);
          return samplePage;
      }
  
  }
  
  
  public class SamplePage extends BasePage {
  
  public SamplePage(WebDriver webDriver){
      super(webDriver);
  }
  
      public void navigateToSite() {
  
          webDriver.get(&quot;https://www.bbc.co.uk&quot;);
          webDriver.findElement(By.xpath(&quot;//a[contains(text(),\'News\')]&quot;)).click();
  
      }
  }
  
  
  public class BasePage extends WorldHelper {
  
      public BasePage(WebDriver driver) {
          this.webDriver = driver;
      }
  }
  </code></pre>
  <p>Any suggestions would be really helpful. Thanks.</p>