-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSeleniumConfig.java
More file actions
546 lines (522 loc) · 22.3 KB
/
SeleniumConfig.java
File metadata and controls
546 lines (522 loc) · 22.3 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package com.nordstrom.automation.selenium;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.json.Json.LIST_OF_MAPS_TYPE;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
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.net.PortProber;
import com.nordstrom.automation.selenium.core.GridServer;
import com.nordstrom.automation.selenium.core.GridUtility;
import com.nordstrom.automation.selenium.utility.HostUtils;
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 DEFAULT_GRID_LAUNCHER = "org.openqa.selenium.grid.Bootstrap";
private static final String DEFAULT_HUB_PORT = "4446";
private static final String DEFAULT_HUB_CONFIG = "hubConfig-s4.json";
private static final String DEFAULT_NODE_CONFIG = "nodeConfig-s4.json";
/**
* <b>org.openqa.selenium.grid.Main</b>
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-grid</artifactId>
* <version>4.9.1</version>
*</dependency></pre>
*
* <b>org.openqa.selenium.io.Zip</b>
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-remote-driver</artifactId>
* <version>4.9.1</version>
*</dependency></pre>
*
* <b>org.openqa.selenium.remote.http.Route</b>
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-http</artifactId>
* <version>4.9.1</version>
*</dependency></pre>
*
* <b>org.openqa.selenium.net.Urls</b>
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-api</artifactId>
* <version>4.9.1</version>
*</dependency></pre>
*
* <b>org.openqa.selenium.json.Json</b>
*
* <pre><dependency>
* <groupId>org.seleniumhq.selenium</groupId>
* <artifactId>selenium-json</artifactId>
* <version>4.9.1</version>
*</dependency></pre>
*
* <b>com.google.common.base.Utf8</b>
*
* <pre><dependency>
* <groupId>com.google.guava</groupId>
* <artifactId>guava</artifactId>
* <version>31.1-jre</version>
*</dependency></pre>
*
* <b>com.beust.jcommander.Strings</b>
*
* <pre><dependency>
* <groupId>com.beust</groupId>
* <artifactId>jcommander</artifactId>
* <version>1.82</version>
*</dependency></pre>
*
* <b>io.opentelemetry.sdk.autoconfigure.SpiUtil</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
* <version>1.25.0-alpha</version>
*</dependency></pre>
*
* <b>io.opentelemetry.sdk.autoconfigure.spi.Ordered</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
* <version>1.25.0</version>
*</dependency></pre>
*
* <b>io.opentelemetry.api.trace.Span</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-api</artifactId>
* <version>1.25.0</version>
*</dependency></pre>
*
* <b>io.opentelemetry.api.logs.Logger</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-api-logs</artifactId>
* <version>1.25.0-alpha</version>
*</dependency></pre>
*
* <b>io.opentelemetry.api.events.EventEmitter</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-api-events</artifactId>
* <version>1.25.0-alpha</version>
*</dependency></pre>
*
* <b>io.opentelemetry.sdk.trace.SdkSpan</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-sdk-trace</artifactId>
* <version>1.25.0</version>
*</dependency></pre>
*
* <b>io.opentelemetry.context.Scope</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-context</artifactId>
* <version>1.25.0</version>
*</dependency></pre>
*
* <b>io.opentelemetry.sdk.metrics.View</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-sdk-metrics</artifactId>
* <version>1.25.0</version>
*</dependency></pre></pre>
*
* <b>io.opentelemetry.sdk.logs.LogLimits</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-sdk-logs</artifactId>
* <version>1.25.0-alpha</version>
*</dependency></pre>
*
* <b>io.opentelemetry.sdk.common.Clock</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-sdk-common</artifactId>
* <version>1.25.0</version>
*</dependency></pre>
*
* <b>io.opentelemetry.semconv.trace.attributes.SemanticAttributes</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-semconv</artifactId>
* <version>1.25.0-alpha</version>
*</dependency></pre>
*
* <b>io.opentelemetry.sdk.OpenTelemetrySdk</b>
*
* <pre><dependency>
* <groupId>io.opentelemetry</groupId>
* <artifactId>opentelemetry-sdk</artifactId>
* <version>1.25.0</version>
*</dependency></pre>
*
* <b>org.zeromq.Utils</b>
*
* <pre><dependency>
* <groupId>org.zeromq</groupId>
* <artifactId>jeromq</artifactId>
* <version>0.5.3</version>
*</dependency></pre>
*
* <b>dev.failsafe.Call</b>
*
* <pre><dependency>
* <groupId>dev.failsafe</groupId>
* <artifactId>failsafe</artifactId>
* <version>3.3.1</version>
*</dependency></pre>
*
* <b>graphql.Assert</b>
*
* <pre><dependency>
* <groupId>com.graphql-java</groupId>
* <artifactId>graphql-java</artifactId>
* <version>20.2</version>
*</dependency></pre>
*
* <b>org.dataloader.DataLoader</b>
*
* <pre><dependency>
* <groupId>com.graphql-java</groupId>
* <artifactId>java-dataloader</artifactId>
* <version>3.2.0</version>
*</dependency></pre>
*
* <b>org.slf4j.MDC</b>
*
* <pre><dependency>
* <groupId>org.slf4j</groupId>
* <artifactId>slf4j-api</artifactId>
* <version>1.7.36</version>
*</dependency></pre>
*
* <b>io.netty.channel.Channel</b>
*
* <pre><dependency>
* <groupId>io.netty</groupId>
* <artifactId>netty-transport</artifactId>
* <version>4.2.0.Final</version>
*</dependency></pre>
*
* <b>io.netty.util.Timer</b>
*
* <pre><dependency>
* <groupId>io.netty</groupId>
* <artifactId>netty-common</artifactId>
* <version>4.2.0.Final</version>
*</dependency></pre>
*
* <b>io.netty.handler.ssl.SslUtils</b>
*
* <pre><dependency>
* <groupId>io.netty</groupId>
* <artifactId>netty-handler</artifactId>
* <version>4.2.0.Final</version>
*</dependency></pre>
*
* <b>org.slf4j.impl.StaticLoggerBinder</b>
*
* <pre><dependency>
* <groupId>ch.qos.logback</groupId>
* <artifactId>logback-classic</artifactId>
* <version>1.2.11</version>
*</dependency></pre>
*
* <b>ch.qos.logback.core.Layout</b>
*
* <pre><dependency>
* <groupId>ch.qos.logback</groupId>
* <artifactId>logback-core</artifactId>
* <version>1.2.11</version>
*</dependency></pre>
*
* <b>io.netty.buffer.ByteBuf</b>
*
* <pre><dependency>
* <groupId>io.netty</groupId>
* <artifactId>netty-buffer</artifactId>
* <version>4.2.0.Final</version>
*</dependency></pre>
*
* <b>io.netty.handler.codec.http.Cookie</b>
*
* <pre><dependency>
* <groupId>io.netty</groupId>
* <artifactId>netty-codec-http</artifactId>
* <version>4.2.0.Final</version>
*</dependency></pre>
*
* <b>io.netty.handler.codec.Headers</b>
*
* <pre><dependency>
* <groupId>io.netty</groupId>
* <artifactId>netty-codec</artifactId>
* <version>4.2.0.Final</version>
*</dependency></pre>
*
* <b>com.google.common.util.concurrent.internal.InternalFutures</b>
*
* <pre><dependency>
* <groupId>com.google.guava</groupId>
* <artifactId>failureaccess</artifactId>
* <version>1.0.1</version>
*</dependency></pre>
*
* <b>io.netty.handler.codec.compression.ZlibCodecFactory</b>
*
* <pre><dependency>
* <groupId>io.netty</groupId>
* <artifactId>netty-codec-compression</artifactId>
* <version>4.2.0.Final</version>
*</dependency></pre>
*/
private static final String[] DEPENDENCY_CONTEXTS = { "com.nordstrom.automation.selenium.core.LocalSeleniumGrid",
"com.nordstrom.common.file.PathUtils", "org.apache.commons.lang3.StringUtils",
"org.eclipse.jetty.util.Attributes", "javax.servlet.http.HttpServletResponse",
"org.eclipse.jetty.http.HttpField", "org.openqa.selenium.grid.Main", "com.beust.jcommander.Strings",
"org.openqa.selenium.remote.http.Route", "com.google.common.base.Utf8", "org.openqa.selenium.Keys",
"org.openqa.selenium.remote.tracing.Tracer", "org.openqa.selenium.json.Json",
"io.opentelemetry.sdk.autoconfigure.ResourceConfiguration",
"io.opentelemetry.sdk.autoconfigure.spi.Ordered", "io.opentelemetry.api.trace.Span",
"io.opentelemetry.sdk.trace.SdkSpan", "io.opentelemetry.context.Scope", "io.opentelemetry.sdk.metrics.View",
"io.opentelemetry.sdk.logs.LogLimits", "io.opentelemetry.sdk.common.Clock",
"io.opentelemetry.sdk.OpenTelemetrySdk", "org.zeromq.Utils", "dev.failsafe.Call", "graphql.Assert",
"org.slf4j.MDC", "io.netty.channel.Channel", "io.netty.util.Timer", "io.netty.handler.ssl.SslUtils",
"io.netty.buffer.ByteBuf", "io.netty.handler.codec.Headers", "io.netty.handler.codec.http.Cookie",
"org.openqa.selenium.io.Zip", "ch.qos.logback.core.Layout", "io.netty.resolver.NameResolver",
"io.opentelemetry.api.logs.Logger", "org.openqa.selenium.net.Urls", "org.dataloader.DataLoader",
"com.google.common.util.concurrent.internal.InternalFutures", "org.eclipse.jetty.server.Server",
"org.reactivestreams.Publisher", "org.openqa.selenium.manager.SeleniumManager",
"org.apache.commons.lang3.Range", "io.netty.buffer.ByteBufUtil",
"io.netty.handler.codec.compression.ZlibCodecFactory", "io.opentelemetry.common.ComponentLoader",
"com.github.benmanes.caffeine.cache.Weigher", "org.openqa.selenium.os.ExternalProcess" };
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 4;
}
/**
* {@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
@SuppressWarnings("unchecked")
public Path createHubConfig() throws IOException {
Map<String, Object> hubConfig;
// get path to hub configuration template
String hubConfigPath = getHubConfigPath().toString();
// create hub configuration from template
try (Reader reader = Files.newBufferedReader(getHubConfigPath())) {
hubConfig = new Json().toType(reader, MAP_TYPE);
} catch (IOException e) {
throw new ConfigException("Failed reading hub configuration template.", e);
}
String slotMatcher = getString(SeleniumSettings.SLOT_MATCHER.key());
// strip extension to get template base path
String configPathBase = hubConfigPath.substring(0, hubConfigPath.length() - 5);
// get hash code of slot matcher as 8-digit hexadecimal string
String hashCode = String.format("%08X", Objects.hash(slotMatcher));
// assemble hub configuration file path with aggregated hash code
Path filePath = Paths.get(configPathBase + "-" + hashCode + ".json");
// if assembled path does not exist
if (filePath.toFile().createNewFile()) {
// add driver configuration
Map<String, Object> distributorOptions = (Map<String, Object>) hubConfig.get("distributor");
distributorOptions.put("slot-matcher", slotMatcher);
try (OutputStream fos = new FileOutputStream(filePath.toFile());
OutputStream out = new BufferedOutputStream(fos)) {
out.write(new Json().toJson(hubConfig).getBytes(UTF_8));
}
}
return filePath;
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public Path createNodeConfig(String capabilities, URL hubUrl) throws IOException {
Map<String, Object> nodeConfig;
boolean isAppium = capabilities.contains("appium");
// get path to node configuration template
String nodeConfigPath = getNodeConfigPath().toString();
// create node configuration from template
try (Reader reader = Files.newBufferedReader(getNodeConfigPath())) {
nodeConfig = new Json().toType(reader, MAP_TYPE);
Map<String, Object> nodeOptions = (Map<String, Object>) nodeConfig.computeIfAbsent("node", k -> new HashMap<>());
nodeOptions.put("hub", hubUrl.getProtocol() + "://" + hubUrl.getAuthority() + GridServer.GRID_REGISTER);
nodeOptions.computeIfAbsent("detect-drivers", k -> false);
// if Appium
if (isAppium) {
// create relay configuration template if absent
Map<String, Object> relayOptions = (Map<String, Object>) nodeConfig.computeIfAbsent("relay", k -> new HashMap<>());
relayOptions.computeIfAbsent("host", k -> HostUtils.getLocalHost());
relayOptions.computeIfAbsent("port", k -> PortProber.findFreePort());
relayOptions.computeIfAbsent("configs", k -> new ArrayList<>());
// otherwise (not Appium)
} else {
// add driver configuration template if absent
nodeOptions.computeIfAbsent("driver-configuration", k -> new ArrayList<>());
}
} catch (IOException e) {
throw new ConfigException("Failed reading node configuration template.", e);
} catch (ClassCastException e) {
throw new ConfigException("Failed unwrapping [node.driver-configuration] option", e);
}
// convert capabilities string to List<Map<String, Object>>
String capsList = (capabilities.startsWith("[")) ? capabilities : "[" + capabilities + "]";
List<Map<String, Object>> capsMapList = new Json().toType(capsList, LIST_OF_MAPS_TYPE);
// convert list of maps into [MutableCapabilities]
// => apply specified node modifications (if any)
List<MutableCapabilities> capabilitiesList = capsMapList.stream()
.map(MutableCapabilities::new)
.map(theseCaps -> theseCaps.merge(getModifications(theseCaps, NODE_MODS_SUFFIX)))
.collect(Collectors.toList());
// convert list of [MutableCapabilities] objects to set of maps
Set<Map<String, Object>> capabilitiesSet = capabilitiesList.stream()
.map(caps -> caps.toJson())
.collect(Collectors.toSet());
// strip extension to get template base path
String configPathBase = nodeConfigPath.substring(0, nodeConfigPath.length() - 5);
// get hash code of capabilities set and hub URL as 8-digit hexadecimal string
String hashCode = String.format("%08X", Objects.hash(capabilitiesSet, hubUrl));
// 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()) {
// if Appium
if (isAppium) {
// add relay slot specification
Map<String, Object> relayOptions = (Map<String, Object>) nodeConfig.get("relay");
List<Object> configs = (List<Object>) relayOptions.get("configs");
capabilitiesList.stream().forEach(theseCaps -> {
configs.add("1");
configs.add(toJson(theseCaps));
});
// otherwise (not Appium)
} else {
// add driver configuration
Map<String, Object> nodeOptions = (Map<String, Object>) nodeConfig.get("node");
List<Object> driverConfiguration = (List<Object>) nodeOptions.get("driver-configuration");
capabilitiesList.stream().forEach(theseCaps -> {
Map<String, Object> thisConfig = new HashMap<>();
thisConfig.put("stereotype", theseCaps);
thisConfig.put("display-name", GridUtility.getPersonality(theseCaps));
Optional.ofNullable(GridUtility.getDriverPath(theseCaps))
.ifPresent(value -> thisConfig.put("webdriver-executable", value));
driverConfiguration.add(thisConfig);
});
}
try (OutputStream fos = new FileOutputStream(filePath.toFile());
OutputStream out = new BufferedOutputStream(fos)) {
out.write(new Json().toJson(nodeConfig).getBytes(UTF_8));
}
}
return filePath;
}
/**
* {@inheritDoc}
*/
@Override
public Capabilities[] getCapabilitiesForJson(String capabilities) {
String capsList = (capabilities.startsWith("[")) ? capabilities : "[" + capabilities + "]";
List<Map<String, Object>> capsMapList = new Json().toType(capsList, LIST_OF_MAPS_TYPE);
return capsMapList.stream().map(MutableCapabilities::new).collect(Collectors.toList()).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);
}
}