WebDriverThreadLocalContainer implements WebDriverContainer { @Override @CheckReturnValue @Nonnull public WebDriver getAndCheckWebDriver() { WebDriver webDriver = threadWebDriver.get(currentThread().getId()); if (webDriver != null && reopenBrowserOnFail && !browserHealthChecker.isBrowserStillOpen(webDriver)) { log.info("Webdriver has been closed meanwhile. Let's re-create it."); closeWebDriver(); webDriver = createDriver(); } else if (webDriver == null) { log.info("No webdriver is bound to current thread: {} - let's create a new webdriver", currentThread().getId()); webDriver = createDriver(); } return webDriver; }  @Override void addListener(WebDriverEventListener listener); @Override void setWebDriver(WebDriver webDriver); @Override void setWebDriver(WebDriver webDriver, @Nullable SelenideProxyServer selenideProxy); @Override void setWebDriver(WebDriver webDriver, @Nullable SelenideProxyServer selenideProxy, DownloadsFolder browserDownloadsFolder); @Override void resetWebDriver(); @Override void setProxy(Proxy userProvidedProxy); @Override @CheckReturnValue boolean hasWebDriverStarted(); @Override @CheckReturnValue @Nonnull WebDriver getWebDriver(); @Override @CheckReturnValue @Nonnull WebDriver getAndCheckWebDriver(); @Nonnull @Override DownloadsFolder getBrowserDownloadsFolder(); @Override @CheckReturnValue @Nullable SelenideProxyServer getProxyServer(); @Override void closeWindow(); @Override void closeWebDriver(); @Override void clearBrowserCache(); @Override @CheckReturnValue @Nonnull String getPageSource(); @Override @CheckReturnValue @Nonnull String getCurrentUrl(); @Override @CheckReturnValue @Nonnull String getCurrentFrameUrl();  }
@Test void shouldNotOpenANewBrowser_ifSettingIsDisabled() { Configuration.reopenBrowserOnFail = false; try { container.getAndCheckWebDriver(); fail("expected IllegalStateException"); } catch (IllegalStateException expected) { assertThat(expected) .hasMessageContaining("reopenBrowserOnFail=false"); } }
@Test void holdsAllBrowsers_toAutomaticallyCloseThem() { WebDriver webDriver = container.getAndCheckWebDriver(); assertThat(webDriver).isNotNull(); assertThat(container.allWebDriverThreads).hasSize(1); assertThat(container.threadWebDriver).hasSize(1); assertThat(container.threadWebDriver.get(container.allWebDriverThreads.iterator().next().getId())).isSameAs(webDriver); assertThat(container.cleanupThreadStarted.get()).isTrue(); }
@Test void doesNotCloseBrowsers_ifHoldBrowserOpenSettingIsTrue() { Configuration.holdBrowserOpen = true; WebDriver webDriver = container.getAndCheckWebDriver(); assertThat(webDriver).isNotNull(); assertThat(container.allWebDriverThreads).hasSize(0); assertThat(container.cleanupThreadStarted.get()).isFalse(); }