Skip to content

Commit ba708ec

Browse files
committed
cp: woot
1 parent 5cc305a commit ba708ec

File tree

3 files changed

+144
-120
lines changed

3 files changed

+144
-120
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package org.openqa.selenium.remote.server;
2+
3+
import static org.openqa.selenium.remote.BrowserType.CHROME;
4+
import static org.openqa.selenium.remote.BrowserType.EDGE;
5+
import static org.openqa.selenium.remote.BrowserType.FIREFOX;
6+
import static org.openqa.selenium.remote.BrowserType.IE;
7+
import static org.openqa.selenium.remote.BrowserType.SAFARI;
8+
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;
9+
import static org.openqa.selenium.remote.DesiredCapabilities.chrome;
10+
import static org.openqa.selenium.remote.DesiredCapabilities.edge;
11+
import static org.openqa.selenium.remote.DesiredCapabilities.firefox;
12+
import static org.openqa.selenium.remote.DesiredCapabilities.htmlUnit;
13+
import static org.openqa.selenium.remote.DesiredCapabilities.internetExplorer;
14+
import static org.openqa.selenium.remote.DesiredCapabilities.opera;
15+
import static org.openqa.selenium.remote.DesiredCapabilities.operaBlink;
16+
import static org.openqa.selenium.remote.DesiredCapabilities.safari;
17+
import static org.openqa.selenium.remote.Dialect.OSS;
18+
import static org.openqa.selenium.remote.Dialect.W3C;
19+
20+
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableMap;
22+
import com.google.common.collect.ImmutableSet;
23+
24+
import org.openqa.selenium.SessionNotCreatedException;
25+
import org.openqa.selenium.remote.Dialect;
26+
27+
import java.nio.file.Path;
28+
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Objects;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
33+
import java.util.stream.Collectors;
34+
35+
/**
36+
* Used to create new {@link ActiveSession} instances as required.
37+
*/
38+
public class ActiveSessionFactory {
39+
40+
private final static Logger LOG = Logger.getLogger(ActiveSessionFactory.class.getName());
41+
42+
private final Map<String, SessionFactory> factories;
43+
44+
public ActiveSessionFactory(DriverSessions legacySessions) {
45+
this.factories = ImmutableMap.<String, SessionFactory>builder()
46+
.put(chrome().getBrowserName(),
47+
new ServicedSession.Factory("org.openqa.selenium.chrome.ChromeDriverService"))
48+
.put(edge().getBrowserName(),
49+
new ServicedSession.Factory("org.openqa.selenium.edge.EdgeDriverService"))
50+
.put(firefox().getBrowserName(),
51+
new ServicedSession.Factory("org.openqa.selenium.firefox.GeckoDriverService"))
52+
.put(htmlUnit().getBrowserName(), new InMemorySession.Factory(legacySessions))
53+
.put(internetExplorer().getBrowserName(),
54+
new ServicedSession.Factory("org.openqa.selenium.ie.InternetExplorerDriverService"))
55+
.put(opera().getBrowserName(),
56+
new ServicedSession.Factory("org.openqa.selenium.ie.OperaDriverService"))
57+
.put(operaBlink().getBrowserName(),
58+
new ServicedSession.Factory("org.openqa.selenium.ie.OperaDriverService"))
59+
.put(safari().getBrowserName(),
60+
new ServicedSession.Factory("org.openqa.selenium.ie.OperaDriverService"))
61+
.build();
62+
}
63+
64+
public ActiveSession createSession(
65+
Path rawCapabilitiesBlob,
66+
Map<String, Object> ossKeys,
67+
Map<String, Object> alwaysMatch,
68+
List<Map<String, Object>> firstMatch) {
69+
List<SessionFactory> browserGenerators = determineBrowser(
70+
ossKeys,
71+
alwaysMatch,
72+
firstMatch);
73+
74+
ImmutableSet.Builder<Dialect> downstreamDialects = ImmutableSet.builder();
75+
// Favour OSS for now
76+
if (!ossKeys.isEmpty()) {
77+
downstreamDialects.add(OSS);
78+
}
79+
if (!alwaysMatch.isEmpty() || !firstMatch.isEmpty()) {
80+
downstreamDialects.add(W3C);
81+
}
82+
83+
return browserGenerators.stream()
84+
.map(func -> {
85+
try {
86+
return func.apply(rawCapabilitiesBlob, downstreamDialects.build());
87+
} catch (Exception e) {
88+
LOG.log(Level.INFO, "Unable to start session.", e);
89+
}
90+
return null;
91+
})
92+
.filter(Objects::nonNull)
93+
.findFirst()
94+
.orElseThrow(() -> new SessionNotCreatedException("Unable to create a new session"));
95+
}
96+
97+
private List<SessionFactory> determineBrowser(
98+
Map<String, Object> ossKeys,
99+
Map<String, Object> alwaysMatchKeys,
100+
List<Map<String, Object>> firstMatchKeys) {
101+
List<Map<String, Object>> allCapabilities = firstMatchKeys.stream()
102+
// remove null keys
103+
.map(caps -> ImmutableMap.<String, Object>builder().putAll(caps).putAll(alwaysMatchKeys)
104+
.build())
105+
.collect(Collectors.toList());
106+
allCapabilities.add(ossKeys);
107+
108+
// Can we figure out the browser from any of these?
109+
ImmutableList.Builder<SessionFactory> builder = ImmutableList.builder();
110+
for (Map<String, Object> caps : allCapabilities) {
111+
caps.entrySet().stream()
112+
.map(entry -> guessBrowserName(entry.getKey(), entry.getValue()))
113+
.filter(factories.keySet()::contains)
114+
.map(factories::get)
115+
.findFirst()
116+
.ifPresent(builder::add);
117+
}
118+
119+
return builder.build();
120+
}
121+
122+
private String guessBrowserName(String capabilityKey, Object value) {
123+
if (BROWSER_NAME.equals(capabilityKey)) {
124+
return (String) value;
125+
}
126+
if ("chromeOptions".equals(capabilityKey)) {
127+
return CHROME;
128+
}
129+
if ("edgeOptions".equals(capabilityKey)) {
130+
return EDGE;
131+
}
132+
if (capabilityKey.startsWith("moz:")) {
133+
return FIREFOX;
134+
}
135+
if (capabilityKey.startsWith("safari.")) {
136+
return SAFARI;
137+
}
138+
if ("se:ieOptions".equals(capabilityKey)) {
139+
return IE;
140+
}
141+
return null;
142+
}
143+
}

java/server/src/org/openqa/selenium/remote/server/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ java_library(
6868
name = 'webdriver-servlet',
6969
srcs = [
7070
'ActiveSession.java',
71+
'ActiveSessionFactory.java',
7172
'ActiveSessions.java',
7273
'BeginSession.java',
7374
'AllHandlers.java',

java/server/src/org/openqa/selenium/remote/server/BeginSession.java

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,16 @@
2121
import static com.google.common.net.MediaType.JSON_UTF_8;
2222
import static java.net.HttpURLConnection.HTTP_OK;
2323
import static java.nio.charset.StandardCharsets.UTF_8;
24-
import static org.openqa.selenium.remote.BrowserType.CHROME;
25-
import static org.openqa.selenium.remote.BrowserType.EDGE;
26-
import static org.openqa.selenium.remote.BrowserType.FIREFOX;
27-
import static org.openqa.selenium.remote.BrowserType.IE;
28-
import static org.openqa.selenium.remote.BrowserType.SAFARI;
2924
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;
30-
import static org.openqa.selenium.remote.DesiredCapabilities.chrome;
31-
import static org.openqa.selenium.remote.DesiredCapabilities.edge;
32-
import static org.openqa.selenium.remote.DesiredCapabilities.firefox;
33-
import static org.openqa.selenium.remote.DesiredCapabilities.htmlUnit;
34-
import static org.openqa.selenium.remote.DesiredCapabilities.internetExplorer;
35-
import static org.openqa.selenium.remote.DesiredCapabilities.opera;
36-
import static org.openqa.selenium.remote.DesiredCapabilities.operaBlink;
37-
import static org.openqa.selenium.remote.DesiredCapabilities.safari;
38-
import static org.openqa.selenium.remote.Dialect.OSS;
39-
import static org.openqa.selenium.remote.Dialect.W3C;
4025

4126
import com.google.common.base.Charsets;
42-
import com.google.common.collect.ImmutableList;
4327
import com.google.common.collect.ImmutableMap;
44-
import com.google.common.collect.ImmutableSet;
4528
import com.google.common.net.MediaType;
4629
import com.google.gson.stream.JsonReader;
4730
import com.google.gson.stream.JsonToken;
4831

4932
import org.openqa.selenium.SessionNotCreatedException;
5033
import org.openqa.selenium.remote.BeanToJsonConverter;
51-
import org.openqa.selenium.remote.Dialect;
5234
import org.openqa.selenium.remote.http.HttpRequest;
5335
import org.openqa.selenium.remote.http.HttpResponse;
5436

@@ -65,15 +47,9 @@
6547
import java.util.LinkedList;
6648
import java.util.List;
6749
import java.util.Map;
68-
import java.util.Objects;
69-
import java.util.logging.Level;
70-
import java.util.logging.Logger;
71-
import java.util.stream.Collectors;
7250

7351
class BeginSession implements CommandHandler {
7452

75-
private final static Logger LOG = Logger.getLogger(BeginSession.class.getName());
76-
7753
private final ActiveSessionFactory sessionFactory;
7854
private final ActiveSessions allSessions;
7955

@@ -247,100 +223,4 @@ private Map<String, Object> sparseCapabilities(JsonReader json) throws IOExcepti
247223
return caps;
248224
}
249225

250-
public static class ActiveSessionFactory {
251-
private final Map<String, SessionFactory> factories;
252-
253-
public ActiveSessionFactory(DriverSessions legacySessions) {
254-
this.factories = ImmutableMap.<String, SessionFactory>builder()
255-
.put(chrome().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.chrome.ChromeDriverService"))
256-
.put(edge().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.edge.EdgeDriverService"))
257-
.put(firefox().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.firefox.GeckoDriverService"))
258-
.put(htmlUnit().getBrowserName(), new InMemorySession.Factory(legacySessions))
259-
.put(internetExplorer().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.ie.InternetExplorerDriverService"))
260-
.put(opera().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.ie.OperaDriverService"))
261-
.put(operaBlink().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.ie.OperaDriverService"))
262-
.put(safari().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.ie.OperaDriverService"))
263-
.build();
264-
}
265-
266-
267-
public ActiveSession createSession(
268-
Path rawCapabilitiesBlob,
269-
Map<String, Object> ossKeys,
270-
Map<String, Object> alwaysMatch,
271-
List<Map<String, Object>> firstMatch) {
272-
List<SessionFactory> browserGenerators = determineBrowser(
273-
ossKeys,
274-
alwaysMatch,
275-
firstMatch);
276-
277-
ImmutableSet.Builder<Dialect> downstreamDialects = ImmutableSet.builder();
278-
// Favour OSS for now
279-
if (!ossKeys.isEmpty()) {
280-
downstreamDialects.add(OSS);
281-
}
282-
if (!alwaysMatch.isEmpty() || !firstMatch.isEmpty()) {
283-
downstreamDialects.add(W3C);
284-
}
285-
286-
return browserGenerators.stream()
287-
.map(func -> {
288-
try {
289-
return func.apply(rawCapabilitiesBlob, downstreamDialects.build());
290-
} catch (Exception e) {
291-
LOG.log(Level.INFO, "Unable to start session.", e);
292-
}
293-
return null;
294-
})
295-
.filter(Objects::nonNull)
296-
.findFirst()
297-
.orElseThrow(() -> new SessionNotCreatedException("Unable to create a new session"));
298-
}
299-
300-
private List<SessionFactory> determineBrowser(
301-
Map<String, Object> ossKeys,
302-
Map<String, Object> alwaysMatchKeys,
303-
List<Map<String, Object>> firstMatchKeys) {
304-
List<Map<String, Object>> allCapabilities = firstMatchKeys.stream()
305-
// remove null keys
306-
.map(caps -> ImmutableMap.<String, Object>builder().putAll(caps).putAll(alwaysMatchKeys).build())
307-
.collect(Collectors.toList());
308-
allCapabilities.add(ossKeys);
309-
310-
// Can we figure out the browser from any of these?
311-
ImmutableList.Builder<SessionFactory> builder = ImmutableList.builder();
312-
for (Map<String, Object> caps : allCapabilities) {
313-
caps.entrySet().stream()
314-
.map(entry -> guessBrowserName(entry.getKey(), entry.getValue()))
315-
.filter(factories.keySet()::contains)
316-
.map(factories::get)
317-
.findFirst()
318-
.ifPresent(builder::add);
319-
}
320-
321-
return builder.build();
322-
}
323-
324-
private String guessBrowserName(String capabilityKey, Object value) {
325-
if (BROWSER_NAME.equals(capabilityKey)) {
326-
return (String) value;
327-
}
328-
if ("chromeOptions".equals(capabilityKey)) {
329-
return CHROME;
330-
}
331-
if ("edgeOptions".equals(capabilityKey)) {
332-
return EDGE;
333-
}
334-
if (capabilityKey.startsWith("moz:")) {
335-
return FIREFOX;
336-
}
337-
if (capabilityKey.startsWith("safari.")) {
338-
return SAFARI;
339-
}
340-
if ("se:ieOptions".equals(capabilityKey)) {
341-
return IE;
342-
}
343-
return null;
344-
}
345-
}
346226
}

0 commit comments

Comments
 (0)