title: Selenium and Parallelized JUnit - WebDriver instances
body: <h2>The setup</h2>
  
  <p>So, basically i am trying to achieve Selenium tests that run in parallel using JUnit.</p>
  
  <p>For that i have found <a href="http://hwellmann.blogspot.de/2009/12/running-parameterized-junit-tests-in.html" rel="nofollow noreferrer">this JUnit runner</a>. It works really well, i like it alot.</p>
  
  <p>However, i am running into problems regarding the handling of WebDriver instances.</p>
  
  <h2>What i want</h2>
  
  <p>Each WebDriver element should be created once for every class before <code>@Test</code> methods are executed. </p>
  
  <p>Logically, i could use the classes constructor for this. Actually this is quite the requirement for my tests because i need to make use of the <code>@Parameters</code>so that i can create the WebDriver instance accordingly (Chrome,FF,IE ...).</p>
  
  <h2>The problem</h2>
  
  <p>The problem is that i want the WebDriver instance to be cleared (<code>driver.quit()</code> ) after <strong>a class</strong> is done and not after <strong>each <code>@Test</code> method</strong> is done. 
  But i cannot use <code>@AfterClass</code> because i cannot make WebDriver a static member, since each class instance has to use its own (otherwise tests would try to run in the same browser).</p>
  
  <h2>A possible solution</h2>
  
  <p>I have found a possible suggestion <a href="https://stackoverflow.com/questions/19067885/static-webdriver-instance-synchronization-in-java">here</a> by <em>Mrunal Gosar</em>. 
  Following his advise i have changed WebDriver to be a <code>static ThreadLocal&lt;WebDriver&gt;</code> instead and then i create instances of it in each constructor using </p>
  
  <pre><code> // in the classes constructor
  
  driver = new ThreadLocal&lt;WebDriver&gt;() {
             @Override
             protected WebDriver initialValue() {
                   return new FirefoxDriver(); /
             }
  };
  </code></pre>
  
  <p>Needles to say that i replaced every <code>driver.whatever</code> call with <code>driver.get().whatever</code> in my code.</p>
  
  <p>Now, to address the ultimate purpose of this i also wrote a <code>@AfterClass</code> method that would call <code>driver.get().quit();</code> which is now accepted by the compiler, as the variable is static.</p>
  
  <p>Testing this however leads to unexpected behavior. I have a Selenium Grid setup with 2 nodes running on a remote machine. I had this setup running as expected before, but now browsers are spammed all over and tests fail. (While 2 browsers should be running instead 8+ are opened)</p>
  
  <p>The thread i linked suggesting this solution had someone commenting that it might be a bad idea to manually handle threads if already using a framework like JUnit.</p>
  
  <h2>My Question</h2>
  
  <p><em>What is the right design to do this?</em></p>
  
  <p>I could only think of</p>
  
  <ol>
  <li>Make what is suggested here work  </li>
  <li>Write a single @Test annotated
  method that executes all other methods and then use @After to
  achieve the same as @AfterClass</li>
  <li>Save the constructor parameter in a member variable and deal with the fact that i have to create a browser before each <code>@Test</code>annotated method is executed (Using <code>@Before</code> to create the WebDriver instance and <code>@After</code> to close the session)</li>
  </ol>
  
  <p>I don't quite know if option 3 runs into possible problems though. If i close the session after each method, then the grid-Server might actually open a new session with an entirely new class on this node before this one has finished the previous ones. While the tests are independent of each other, i still feel like this is potential danger. </p>
  
  <p>Is anyone on here actively using a multithreaded Selenium test suit and can guide me what is proper design? </p>