View Javadoc

1   package com.atlassian.webdriver.utils;
2   
3   import org.openqa.selenium.WebDriver;
4   import org.openqa.selenium.chrome.ChromeDriver;
5   import org.openqa.selenium.firefox.FirefoxDriver;
6   import org.openqa.selenium.htmlunit.HtmlUnitDriver;
7   import org.openqa.selenium.ie.InternetExplorerDriver;
8   import org.openqa.selenium.internal.WrapsDriver;
9   
10  import javax.annotation.Nonnull;
11  
12  import static com.google.common.base.Preconditions.checkNotNull;
13  
14  /**
15   * @since 2.1
16   */
17  public final class WebDriverUtil
18  {
19      private WebDriverUtil()
20      {
21          throw new AssertionError(WebDriverUtil.class.getName() + " is not supposed to be instantiated");
22      }
23  
24      public static boolean isFirefox(@Nonnull WebDriver driver)
25      {
26          return getUnderlyingDriver(driver) instanceof FirefoxDriver;
27      }
28  
29      public static boolean isChrome(@Nonnull WebDriver driver)
30      {
31          return getUnderlyingDriver(driver) instanceof ChromeDriver;
32      }
33  
34      public static boolean isHtmlUnit(@Nonnull WebDriver driver)
35      {
36          return getUnderlyingDriver(driver) instanceof HtmlUnitDriver;
37      }
38  
39      public static boolean isIE(@Nonnull WebDriver driver)
40      {
41          return getUnderlyingDriver(driver) instanceof InternetExplorerDriver;
42      }
43  
44      /**
45       * Retrieve the underlying {@link WebDriver} instance.
46       *
47       * @param driver the driver to examine
48       * @return the underlying web driver instance that is actually driving the test. This can be the same instance
49       * as {@literal driver}, or some other instance that is wrapped by {@literal driver}.
50       */
51      @Nonnull
52      public static WebDriver getUnderlyingDriver(@Nonnull WebDriver driver)
53      {
54          checkNotNull(driver, "driver");
55          while (driver instanceof WrapsDriver)
56          {
57              driver = WrapsDriver.class.cast(driver).getWrappedDriver();
58          }
59          return driver;
60      }
61  
62      /**
63       * Check whether the underlying driver is instance of {@literal type}. This method attempts to retrieve the underlying
64       * web driver before performing the instance check.
65       *
66       * @param driver driver to check
67       * @param type type to check
68       * @return {@literal true}, if the underlying driver is an instance of {@literal type}, in which case
69       * {@link #as(WebDriver, Class)} may be safely used on it
70       * @see #getUnderlyingDriver(WebDriver)
71       */
72      public static boolean isInstance(@Nonnull WebDriver driver, @Nonnull Class<?> type)
73      {
74          return type.isInstance(getUnderlyingDriver(driver));
75      }
76  
77      /**
78       * Get the underlying web driver instance from {@literal driver} as instance of {@literal type}.
79       *
80       * @param driver driver to examine
81       * @param type type to cast to
82       * @param <T> type parameter
83       * @return the underlying driver instance as T
84       * @throws ClassCastException if {@link #isInstance(WebDriver, Class)} for given arguments returns {@literal false}
85       * @see #isInstance(org.openqa.selenium.WebDriver, Class)
86       */
87      public static <T> T as(@Nonnull WebDriver driver, @Nonnull Class<T> type)
88      {
89          return type.cast(getUnderlyingDriver(driver));
90      }
91  }