/** Creates a {@link WebDriver} for each test, then make sure to clean it up at the end. */
  @Provides
  @TestScope
  public WebDriver createWebDriver(TestCleaner cleaner, TestName testName, ElasticTime time)
      throws IOException {
    WebDriver base = createWebDriver(testName);
    Dimension oldSize = base.manage().window().getSize();
    if (oldSize.height < 768 || oldSize.width < 1024) {
      base.manage().window().setSize(new Dimension(1024, 768));
    }

    final EventFiringWebDriver d = new EventFiringWebDriver(base);
    d.register(new SanityChecker());
    d.register(new Scroller());

    try {
      d.manage().timeouts().pageLoadTimeout(time.seconds(30), TimeUnit.MILLISECONDS);
      d.manage().timeouts().implicitlyWait(time.seconds(1), TimeUnit.MILLISECONDS);
    } catch (UnsupportedCommandException e) {
      // sauce labs RemoteWebDriver doesn't support this
      System.out.println(base + " doesn't support page load timeout");
    }
    cleaner.addTask(
        new Statement() {
          @Override
          public void evaluate() throws Throwable {
            d.quit();
          }
        });
    return d;
  }
  private FirefoxDriver getNegotiatingFirefox(KerberosContainer kdc, String tokenCache) {
    FirefoxProfile profile = new FirefoxProfile();
    profile.setAlwaysLoadNoFocusLib(true);
    // Allow auth negotiation for jenkins under test
    profile.setPreference("network.negotiate-auth.trusted-uris", jenkins.url.toExternalForm());
    profile.setPreference("network.negotiate-auth.delegation-uris", jenkins.url.toExternalForm());
    FirefoxBinary binary = new FirefoxBinary();
    // Inject config and TGT
    binary.setEnvironmentProperty("KRB5CCNAME", tokenCache);
    binary.setEnvironmentProperty("KRB5_CONFIG", kdc.getKrb5ConfPath());
    // Turn debug on
    binary.setEnvironmentProperty("KRB5_TRACE", diag.touch("tracelog").getAbsolutePath());
    binary.setEnvironmentProperty("NSPR_LOG_MODULES", "negotiateauth:5");
    binary.setEnvironmentProperty(
        "NSPR_LOG_FILE", diag.touch("firefox.nego.log").getAbsolutePath());

    String display = FallbackConfig.getBrowserDisplay();
    if (display != null) {
      binary.setEnvironmentProperty("DISPLAY", display);
    }
    final FirefoxDriver driver = new FirefoxDriver(binary, profile);
    cleaner.addTask(
        new Statement() {
          @Override
          public void evaluate() throws Throwable {
            try {
              driver.quit();
            } catch (UnreachableBrowserException ex) {
              System.err.println("Browser died already");
              ex.printStackTrace();
            }
          }

          @Override
          public String toString() {
            return "Close Kerberos WebDriver after test";
          }
        });
    return driver;
  }