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
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
49
50
51
52
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
67
68
69
70
71
72
73
74
75 public static boolean isInstance(@Nonnull WebDriver driver, @Nonnull Class<?> type)
76 {
77 return type.isInstance(getUnderlyingDriver(driver));
78 }
79
80
81
82
83
84
85
86
87
88
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
97
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 }