-
Notifications
You must be signed in to change notification settings - Fork 11
/
DefaultWebDriverSupplier.java
151 lines (129 loc) · 5.04 KB
/
DefaultWebDriverSupplier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.xebia.incubator.xebium;
import com.opera.core.systems.OperaDriver;
import com.opera.core.systems.OperaProduct;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.PreferencesWrapper;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.safari.SafariDriver;
import org.sikuli.webdriver.SikuliChromeDriver;
import org.sikuli.webdriver.SikuliFirefoxDriver;
import org.sikuli.webdriver.SikuliIExploreDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
public class DefaultWebDriverSupplier implements ConfigurableWebDriverSupplier {
private static final Logger LOG = LoggerFactory.getLogger(SeleniumDriverFixture.class);
public String browser;
private File customProfilePreferencesFile;
private File profileDirectory;
public DefaultWebDriverSupplier() {
}
public WebDriver newWebDriver() {
WebDriver driver;
if ("firefox".equalsIgnoreCase(browser)) {
FirefoxProfile profile;
// Load FireFox-profile if present
if (profileDirectory != null) {
profile = new FirefoxProfile(profileDirectory);
LOG.info("Firefox profile successfully loaded");
}
else {
profile = new FirefoxProfile();
}
if (customProfilePreferencesFile != null) {
PreferencesWrapper prefs = loadFirefoxPreferences();
prefs.addTo(profile);
try {
StringWriter writer = new StringWriter(512);
prefs.writeTo(writer);
LOG.info("Added properties to firefox profile: " + writer.toString());
} catch (IOException e) {
LOG.error("Unable to log firefox profile settings", e);
}
}
// Ensure we deal with untrusted and unverified hosts.
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
driver = new FirefoxDriver(profile);
} else if ("iexplore".equalsIgnoreCase(browser)) {
driver = new InternetExplorerDriver();
} else if ("sikuli iexplore".equalsIgnoreCase(browser)) {
driver = new SikuliIExploreDriver();
} else if ("sikuli firefox".equalsIgnoreCase(browser)) {
driver = new SikuliFirefoxDriver();
} else if ("sikuli chrome".equalsIgnoreCase(browser)) {
driver = new SikuliChromeDriver();
} else if ("chrome".equalsIgnoreCase(browser)) {
driver = new ChromeDriver();
} else if ("safari".equalsIgnoreCase(browser)) {
driver = new SafariDriver();
} else if ("htmlUnit".equalsIgnoreCase(browser)) {
driver = new HtmlUnitDriver();
} else if ("htmlUnit+js".equalsIgnoreCase(browser)) {
driver = new HtmlUnitDriver(true);
} else if ("opera".equalsIgnoreCase(browser)) {
driver = new OperaDriver();
} else if ("opera-mobile-tablet".equalsIgnoreCase(browser)) {
DesiredCapabilities capabilities = DesiredCapabilities.opera();
// tell opera mobile to use the tablet ui
capabilities.setCapability("opera.product", OperaProduct.MOBILE);
capabilities.setCapability("opera.arguments", "-tabletui -displaysize 860x600");
driver = new OperaDriver(capabilities);
} else if ("opera-mobile-phone".equalsIgnoreCase(browser)) {
DesiredCapabilities capabilities = DesiredCapabilities.opera();
// tell opera mobile to use the mobile handset ui
capabilities.setCapability("opera.product", OperaProduct.MOBILE);
capabilities.setCapability("opera.arguments", "-mobileui");
driver = new OperaDriver(capabilities);
} else if ("phantomjs".equalsIgnoreCase(browser)) {
driver = new PhantomJSDriver(DesiredCapabilities.phantomjs());
} else {
try {
driver = new RemoteWebDriverSupplier(browser).get();
} catch (Exception e) {
throw new RuntimeException("Unknown browser type. Should be one of 'firefox', 'iexplore', 'chrome', " +
"'opera', 'opera-mobile-tablet', 'opera-mobile-phone', 'htmlUnit' or 'htmlUnit+js'", e);
}
}
return driver;
}
private PreferencesWrapper loadFirefoxPreferences() {
PreferencesWrapper prefs;
FileReader reader;
try {
reader = new FileReader(customProfilePreferencesFile);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
try {
prefs = new PreferencesWrapper(reader);
} finally {
try {
reader.close();
} catch (IOException e) {
LOG.error("Unable to close firefox profile settings file", e);
}
}
return prefs;
}
public void setBrowser(String browser) {
this.browser = browser;
}
public void setCustomProfilePreferencesFile(
File customProfilePreferencesFile) {
this.customProfilePreferencesFile = customProfilePreferencesFile;
}
public void setProfileDirectory(File profileDirectory) {
this.profileDirectory = profileDirectory;
}
@Override
public String toString() {
return getClass().getName();
}
}