View Javadoc

1   package com.atlassian.webdriver.testing.rule;
2   
3   import com.atlassian.webdriver.AtlassianWebDriver;
4   import com.atlassian.webdriver.browsers.WebDriverBrowserAutoInstall;
5   import com.google.common.base.Supplier;
6   import com.google.common.base.Suppliers;
7   import org.openqa.selenium.WebDriver;
8   
9   import javax.annotation.Nonnull;
10  
11  import static com.google.common.base.Preconditions.checkNotNull;
12  
13  /**
14   * <p/>
15   * Support for rules that need a {@link org.openqa.selenium.WebDriver} instance.
16   *
17   * <p/>
18   * This class defines 'templates' for constructors to provide the WebDriver instance in a generic way. By default it
19   * falls back to {@link com.atlassian.webdriver.LifecycleAwareWebDriverGrid}.
20   *
21   * @since 2.1
22   */
23  public final class WebDriverSupport<WD extends WebDriver>
24  {
25      public static WebDriverSupport<AtlassianWebDriver> fromAutoInstall()
26      {
27          return new WebDriverSupport<AtlassianWebDriver>(WebDriverBrowserAutoInstall.driverSupplier());
28      }
29  
30      public static <VE extends WebDriver> WebDriverSupport<VE> forSupplier(Supplier<? extends VE> driverSupplier)
31      {
32          return new WebDriverSupport<VE>(driverSupplier);
33      }
34  
35      public static <VE extends WebDriver> WebDriverSupport<VE> forInstance(VE webDriver)
36      {
37          return new WebDriverSupport<VE>(Suppliers.ofInstance(checkNotNull(webDriver, "webDriver")));
38      }
39  
40      private final Supplier<? extends WD> driverSupplier;
41  
42      private WebDriverSupport(@Nonnull Supplier<? extends WD> driverSupplier)
43      {
44          this.driverSupplier = Suppliers.memoize(checkNotNull(driverSupplier, "driverSupplier"));
45      }
46  
47      @Nonnull
48      public final WD getDriver()
49      {
50          return driverSupplier.get();
51      }
52  
53  }