title: Best Way to Reset Browser State in TestNG with Selenium and Java
body: <p>Can someone recommend the best way to 'tearDown' in the @AfterClass with testng? I occasionally get a hung window or popup and can't get back to the homepage to logout gracefully for the next class to start and avoid modal dialog errors. </p>
  
  <p>I've tried getting a window handle on the homepage, then one on the popup, then putting a switchTo in my @AfterClass but it doesn't work. I've also tried driver.close(). But then my next class will be skipped with an unreachable browser error.</p>
  
  <p>I really need a good tearDown @AfterClass method that can get out of whatever errors and popups are on page, and just leave a clean browser window at login page for the next test class to run.</p>
  
  <p>Edit: Adding Code:</p>
  
  <pre><code>package TestPackage;
  
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.testng.Assert;
  import org.testng.annotations.*;
  import org.testng.annotations.Test;    
  
  //@Test (priority = 1)
  public class test1 {
      public static WebDriver driver = new FirefoxDriver();
      Environment environment = Environment.QA03();
      User testUser = User.ns_system();
      AxUtilities axUtilities;
  
      @BeforeClass
      @Parameters("environment")
      @Test
      public void login(String environment1){
  
  //        environment = new Environment(environment1);
          axUtilities  = new DAxUtilities(environment1, driver);
  //        DAxUtilities dAxUtilities = new DAxUtilities(environment);
          Login login = new Login();
          login.getLogin(testUser, axUtilities, driver);
          axUtilities.sleep(5000);
      }
  
      @Test
      public void testNothing(){
          String s = "public void testNothing() reached...";
          System.out.println(s);
      }
  
  
      @AfterClass
      public void verifyOKAndLogout() {
  
          driver.quit();
  //        DAxUtilities dAxUtilities;
  
      }
  
  }
  </code></pre>
  
  <p>test class 1 and 2 are the same except for the class name...</p>
  
  <p>And the tests are run from xml file. And I've tried many variants of the xml file, even putting each test in its own xml file:</p>
  
  <pre><code>&lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" &gt;
  
  &lt;suite name="TestSuite1" verbose="1"&gt;
      &lt;parameter name="environment" value="QA03"/&gt;
  
  &lt;test name="Test1"&gt;
      &lt;classes&gt;
          &lt;class name="TestPackage.test1"/&gt;
          &lt;!--&lt;class name="TestPackage.test2"/&gt;--&gt;
      &lt;/classes&gt;
  &lt;/test&gt;
  &lt;test name="Test2"&gt;
      &lt;classes&gt;
          &lt;class name="TestPackage.test2"/&gt;
      &lt;/classes&gt;
  &lt;/test&gt;
  
  &lt;/suite&gt;
  </code></pre>
  
  <p>But always two web drivers get created, one right after the other, at the very beginning of the test run, before any other code is executed. And breakpoints show the code is definitely skipping from the beginning of class 1, to the beginning of class 2, before running class 1 tests...</p>