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
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
46
47
48
49
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
64
65
66
67
68
69
70
71
72 public static boolean isInstance(@Nonnull WebDriver driver, @Nonnull Class<?> type)
73 {
74 return type.isInstance(getUnderlyingDriver(driver));
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public static <T> T as(@Nonnull WebDriver driver, @Nonnull Class<T> type)
88 {
89 return type.cast(getUnderlyingDriver(driver));
90 }
91 }