title: Restart browser between tests
body: <p>How to restart browser between tests?
  I have DrivErFactory and in the BasePage in constructor I call</p>
  
  <pre><code>public BasePage() {
      this.driver = DriverFactory.getDriver();
      this.wait = new WebDriverWait(getDriver(), waitTime);
  }
  </code></pre>
  
  <p>Then in my tests I have:</p>
  
  <pre><code>@BeforeMethod
  public void beforeTest() {
  loginPage.open();}
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@AfterMethod
      public void afterTest() {
          loginPage.quitDriver();
      }
  </code></pre>
  
  <p><strong>After first test</strong> I get:
  <em>org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?</em></p>
  
  <p>If I use in After:</p>
  
  <pre><code>loginPage.closeDriver();
  </code></pre>
  
  <p>I have:
  <em>org.openqa.selenium.NoSuchSessionException: no such session</em></p>
  
  <p>In <em>BeforeTest</em> WebDriver can start so why he doesn't start after <em>AfterTest</em>?</p>
  
  <p>How to do better? How to restart browser before each test?</p>
  
  <p><strong>DriverFactory</strong></p>
  
  <pre><code>public class DriverFactory {
      static PropertyBase propertyBase = new PropertyBase();
      Properties prop = new Properties();
  
      private static WebDriver driver;
  
      private static void setChromeDriverPath() {
          String chromeDriverPath = ".\\resources\\chromedriver.exe";
          System.setProperty("webdriver.chrome.driver", chromeDriverPath);
      }
  
      private static void setFireFoxDriverPath() {
          String chromeDriverPath = ".\\resources\\geckodriver.exe";
          System.setProperty("webdriver.gecko.driver", chromeDriverPath);
      }
  
      private static void configureDriver() {
          driver.manage().deleteAllCookies();
          driver.manage().window().maximize();
      }
  
      private static FirefoxProfile setFireFoxProfile() {
          FirefoxProfile profile = new FirefoxProfile();
          profile.setPreference("browser.download.dir", "./downloads");
          profile.setPreference("browser.download.folderList", 2);
          profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
              "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); 
          profile.setPreference( "browser.download.manager.showWhenStarting", false );
          profile.setPreference( "pdfjs.disabled", true );
          return profile;
      }
  
      private static ChromeOptions setChromeProfile() {
          ChromeOptions options = new ChromeOptions();
          Map&lt;String, Object&gt; prefs = new HashMap&lt;String, Object&gt;();
          prefs.put("download.prompt_for_download", true);
          prefs.put("download.default_directory", "./download");
          options.setExperimentalOption("prefs", prefs);
          return options;
      }
  
      private static WebDriver createChromeDriver(){
          setChromeDriverPath();
          return driver = new ChromeDriver(setChromeProfile());
      }
  
      private static WebDriver createFireFoxDriver(){
          setFireFoxDriverPath();
          return driver = new FirefoxDriver(setFireFoxProfile());
      }
  
      public static WebDriver getDriver() {
          String browserType = propertyBase.getProperty("browser");
  
          switch (browserType) {
          case "chrome":
              if (driver==null) {
                  driver = createChromeDriver();
              }
              break;
          case "firefox":
              if (driver==null) {
                  createFireFoxDriver();
              }
              break;
          }
          configureDriver();
          return driver;
      }
  </code></pre>