View Javadoc

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