-
Notifications
You must be signed in to change notification settings - Fork 16
/
SeleniumConfig.java
346 lines (322 loc) · 14 KB
/
SeleniumConfig.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.nordstrom.automation.selenium;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.openqa.grid.internal.utils.CapabilityMatcher;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.web.servlet.LifecycleServlet;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.grid.config.ConfigException;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
import com.nordstrom.automation.settings.SettingsCore;
/**
* This class declares settings and methods related to WebDriver and Grid configuration specific to the Selenium 3 API.
*
* @see SettingsCore
*/
public class SeleniumConfig extends AbstractSeleniumConfig {
private static final String JSON_HEAD = "{ \"capabilities\": [";
private static final String JSON_TAIL = "] }";
private static final String DEFAULT_GRID_LAUNCHER = "org.openqa.grid.selenium.GridLauncherV3";
private static final String DEFAULT_HUB_PORT = "4445";
private static final String DEFAULT_HUB_CONFIG = "hubConfig-s3.json";
private static final String DEFAULT_NODE_CONFIG = "nodeConfig-s3.json";
/**
* <b>org.openqa.grid.selenium.GridLauncherV3</b>
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-server</artifactId>
* <version>3.141.59</version>
* <exclusions>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-chrome-driver</artifactId>
* </exclusion>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-edge-driver</artifactId>
* </exclusion>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-firefox-driver</artifactId>
* </exclusion>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-ie-driver</artifactId>
* </exclusion>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-opera-driver</artifactId>
* </exclusion>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-java</artifactId>
* </exclusion>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-safari-driver</artifactId>
* </exclusion>
* <exclusion>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>htmlunit-driver</artifactId>
* </exclusion>
* <exclusion>
* <groupId>net.sourceforge.htmlunit</groupId>
* <artifactId>htmlunit</artifactId>
* </exclusion>
* </exclusions>
*</dependency></pre>
*
* <b>org.openqa.selenium.BuildInfo</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-api</artifactId>
* <version>3.141.59</version>
*</dependency></pre>
*
* <b>com.google.common.collect.ImmutableMap</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>com.google.guava</groupId>
* <artifactId>guava</artifactId>
* <version>25.0-jre</version>
*</dependency></pre>
*
* <b>com.beust.jcommander.JCommander</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>com.beust</groupId>
* <artifactId>jcommander</artifactId>
* <version>1.72</version>
*</dependency></pre>
*
* <b>org.openqa.selenium.json.Json</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-remote-driver</artifactId>
* <version>3.141.59</version>
*</dependency></pre>
*
* <b>org.seleniumhq.jetty9.util.thread.ThreadPool</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>jetty-repacked</artifactId>
* <version>9.4.12v20180830</version>
*</dependency></pre>
*
* <b>javax.servlet.Servlet</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>javax.servlet</groupId>
* <artifactId>javax-servlet-api</artifactId>
* <version>3.1.0</version>
*</dependency></pre>
*
* <b>okhttp3.ConnectionPool</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>com.squareup.okhttp3</groupId>
* <artifactId>okhttp3</artifactId>
* <version>3.11.0</version>
*</dependency></pre>
*
* <b>okio.BufferedSource</b> (for selenium-server)
*
* <pre><dependency>
* <groupId>com.squareup.okio</groupId>
* <artifactId>okio</artifactId>
* <version>1.14.0</version>
*</dependency></pre>
*
* <b>ch.qos.logback.classic.spi.ThrowableProxy</b>
*
* <pre><dependency>
* <groupId>ch.qos.logback</groupId>
* <artifactId>logback-classic</artifactId>
* <version>1.2.3</version>
*</dependency></pre>
*
* <b>kotlin.jvm.internal.Intrinsics</b>
*
* <pre><dependency>
* <groupId>org.jetbrains.kotlin</groupId>
* <artifactId>kotlin-stdlib</artifactId>
* <version>1.4.10</version>
*</dependency></pre>
*/
private static final String[] DEPENDENCY_CONTEXTS = { "com.nordstrom.automation.selenium.core.LocalSeleniumGrid",
"com.nordstrom.common.file.PathUtils", "org.apache.commons.lang3.reflect.FieldUtils",
"net.bytebuddy.matcher.ElementMatcher", "org.openqa.selenium.BuildInfo",
"com.google.common.collect.ImmutableMap", "com.beust.jcommander.JCommander",
"org.openqa.selenium.json.Json", "org.seleniumhq.jetty9.util.thread.ThreadPool", "javax.servlet.Servlet",
"okhttp3.ConnectionPool", "okio.BufferedSource", "ch.qos.logback.classic.spi.ThrowableProxy",
"kotlin.jvm.internal.Intrinsics", "org.apache.commons.exec.Executor" };
static {
try {
seleniumConfig = new SeleniumConfig();
} catch (ConfigurationException | IOException e) {
throw new RuntimeException("Failed to instantiate settings", e);
}
}
/**
* Instantiate a <b>Selenium Foundation</b> configuration object.
*
* @throws ConfigurationException If a failure is encountered while initializing this configuration object.
* @throws IOException If a failure is encountered while reading from a configuration input stream.
*/
public SeleniumConfig() throws ConfigurationException, IOException {
super();
}
/**
* Get the Selenium configuration object.
*
* @return Selenium configuration object
*/
public static SeleniumConfig getConfig() {
return seleniumConfig;
}
/**
* {@inheritDoc}
*/
@Override
public int getVersion() {
return 3;
}
/**
* {@inheritDoc}
*/
@Override
protected Map<String, String> getDefaults() {
Map<String, String> defaults = super.getDefaults();
defaults.put(SeleniumSettings.GRID_LAUNCHER.key(), DEFAULT_GRID_LAUNCHER);
defaults.put(SeleniumSettings.LAUNCHER_DEPS.key(), StringUtils.join(DEPENDENCY_CONTEXTS, File.pathSeparator));
defaults.put(SeleniumSettings.HUB_PORT.key(), DEFAULT_HUB_PORT);
defaults.put(SeleniumSettings.HUB_CONFIG.key(), DEFAULT_HUB_CONFIG);
defaults.put(SeleniumSettings.NODE_CONFIG.key(), DEFAULT_NODE_CONFIG);
return defaults;
}
/**
* {@inheritDoc}
*/
@Override
public Path createHubConfig() throws IOException {
// get path to hub configuration template
String hubConfigPath = getHubConfigPath().toString();
// create hub configuration from template
GridHubConfiguration hubConfig = GridHubConfiguration.loadFromJSON(hubConfigPath);
String slotMatcher = getString(SeleniumSettings.SLOT_MATCHER.key());
// get configured grid servlet collection
Set<String> servlets = getGridServlets();
// merge with hub template servlets
servlets.addAll(hubConfig.servlets);
// strip extension to get template base path
String configPathBase = hubConfigPath.substring(0, hubConfigPath.length() - 5);
// get hash code of slot matcher and servlets as 8-digit hexadecimal string
String hashCode = String.format("%08X", Objects.hash(slotMatcher, servlets));
// assemble hub configuration file path with servlets hash code
Path filePath = Paths.get(configPathBase + "-" + hashCode + ".json");
// if assembled path does not exist
if (filePath.toFile().createNewFile()) {
try {
hubConfig.capabilityMatcher = (CapabilityMatcher) Class.forName(slotMatcher).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new ConfigException("Failed instantiating capability matcher: " + slotMatcher, e);
}
hubConfig.servlets = Arrays.asList(servlets.toArray(new String[0]));
try(OutputStream fos = new FileOutputStream(filePath.toFile());
OutputStream out = new BufferedOutputStream(fos)) {
out.write(new Json().toJson(hubConfig).getBytes(StandardCharsets.UTF_8));
}
}
return filePath;
}
/**
* {@inheritDoc}
*/
@Override
public Path createNodeConfig(String capabilities, URL hubUrl) throws IOException {
// get path to node configuration template
String nodeConfigPath = getNodeConfigPath().toString();
// create node configuration from template
GridNodeConfiguration nodeConfig = GridNodeConfiguration.loadFromJSON(nodeConfigPath);
// convert capabilities string to [JsonInput] object
JsonInput input = new Json().newInput(new StringReader(JSON_HEAD + capabilities + JSON_TAIL));
// convert [JsonInput] object to list of [MutableCapabilities] objects
List<MutableCapabilities> capabilitiesList = GridNodeConfiguration.loadFromJSON(input).capabilities;
// for each [MutableCapabilities] object
for (MutableCapabilities theseCaps : capabilitiesList) {
// apply specified node modifications (if any)
theseCaps.merge(getModifications(theseCaps, NODE_MODS_SUFFIX));
}
// get configured node servlet collection
Set<String> servlets = new HashSet<>(nodeConfig.servlets);
// if remote shutdown feature is specified
if (getBoolean(SeleniumSettings.GRID_LIFECYCLE.key())) {
// add lifecycle servlet to the collection
servlets.add(LifecycleServlet.class.getName());
}
// strip extension to get template base path
String configPathBase = nodeConfigPath.substring(0, nodeConfigPath.length() - 5);
// get hash code of capabilities list, hub URL, and servlets as 8-digit hexadecimal string
String hashCode = String.format("%08X", Objects.hash(capabilitiesList, hubUrl, servlets));
// assemble node configuration file path with aggregated hash code
Path filePath = Paths.get(configPathBase + "-" + hashCode + ".json");
// if assembled path does not exist
if (filePath.toFile().createNewFile()) {
nodeConfig.hub = null;
nodeConfig.capabilities = capabilitiesList;
nodeConfig.hubHost = hubUrl.getHost();
nodeConfig.hubPort = hubUrl.getPort();
nodeConfig.servlets = Arrays.asList(servlets.toArray(new String[0]));
try(OutputStream fos = new FileOutputStream(filePath.toFile());
OutputStream out = new BufferedOutputStream(fos)) {
out.write(new Json().toJson(nodeConfig).getBytes(StandardCharsets.UTF_8));
}
}
return filePath;
}
/**
* {@inheritDoc}
*/
@Override
public Capabilities[] getCapabilitiesForJson(String capabilities) {
JsonInput input = new Json().newInput(new StringReader(JSON_HEAD + capabilities + JSON_TAIL));
return GridNodeConfiguration.loadFromJSON(input).capabilities.toArray(new Capabilities[0]);
}
/**
* {@inheritDoc}
*/
@Override
public Capabilities mergeCapabilities(Capabilities target, Capabilities change) {
return target.merge(change);
}
/**
* {@inheritDoc}
*/
@Override
public String toJson(Object obj) {
return new Json().toJson(obj);
}
}