21
21
import java .util .Map ;
22
22
import java .util .Objects ;
23
23
import java .util .ServiceLoader ;
24
+ import java .util .function .Function ;
24
25
import java .util .function .Predicate ;
25
26
import java .util .logging .Logger ;
26
27
import java .util .regex .Pattern ;
@@ -34,12 +35,11 @@ public class ActiveSessionFactory {
34
35
35
36
private final static Logger LOG = Logger .getLogger (ActiveSessionFactory .class .getName ());
36
37
37
- private final static Predicate <String > CLASS_EXISTS = name -> {
38
+ private final static Function <String , Class <?> > CLASS_EXISTS = name -> {
38
39
try {
39
- Class .forName (name );
40
- return true ;
40
+ return Class .forName (name );
41
41
} catch (ClassNotFoundException cnfe ) {
42
- return false ;
42
+ return null ;
43
43
}
44
44
};
45
45
@@ -48,6 +48,16 @@ public class ActiveSessionFactory {
48
48
public ActiveSessionFactory () {
49
49
Map <Predicate <Capabilities >, SessionFactory > builder = new LinkedHashMap <>();
50
50
51
+ bind (
52
+ builder ,
53
+ "org.openqa.selenium.firefox.FirefoxDriver" ,
54
+ caps -> {
55
+ Object marionette = caps .getCapability ("marionette" );
56
+
57
+ return marionette instanceof Boolean && !(Boolean ) marionette ;
58
+ },
59
+ firefox ());
60
+
51
61
ImmutableMap .<Predicate <Capabilities >, String >builder ()
52
62
.put (browserName (chrome ()), "org.openqa.selenium.chrome.ChromeDriverService" )
53
63
.put (containsKey ("chromeOptions" ), "org.openqa.selenium.chrome.ChromeDriverService" )
@@ -64,21 +74,11 @@ public ActiveSessionFactory() {
64
74
.put (containsKey (Pattern .compile ("^safari\\ ..*" )), "org.openqa.selenium.safari.SafariDriverService" )
65
75
.build ()
66
76
.entrySet ().stream ()
67
- .filter (e -> CLASS_EXISTS .test (e .getValue ()))
77
+ .filter (e -> CLASS_EXISTS .apply (e .getValue ()) != null )
68
78
.forEach (e -> builder .put (e .getKey (), new ServicedSession .Factory (e .getValue ())));
69
79
70
80
// Attempt to bind the htmlunitdriver if it's present.
71
- try {
72
- Class <? extends WebDriver >
73
- clazz =
74
- Class .forName ("org.openqa.selenium.htmlunit.HtmlUnitDriver" )
75
- .asSubclass (WebDriver .class );
76
- builder .put (
77
- browserName (htmlUnit ()),
78
- new InMemorySession .Factory (new DefaultDriverProvider (htmlUnit (), clazz )));
79
- } catch (ReflectiveOperationException ignored ) {
80
- // Just carry on. Everything is fine.
81
- }
81
+ bind (builder , "org.openqa.selenium.htmlunit.HtmlUnitDriver" , browserName (htmlUnit ()), htmlUnit ());
82
82
83
83
// Allow user-defined factories to override default ones
84
84
StreamSupport .stream (ServiceLoader .load (DriverProvider .class ).spliterator (), false )
@@ -92,7 +92,7 @@ public ActiveSessionFactory() {
92
92
"org.openqa.selenium.edge.EdgeDriverService" ,
93
93
"org.openqa.selenium.ie.InternetExplorerDriverService" ,
94
94
"org.openqa.selenium.safari.SafariDriverService" )
95
- .filter (CLASS_EXISTS )
95
+ .filter (name -> CLASS_EXISTS . apply ( name ) != null )
96
96
.findFirst ()
97
97
.ifPresent (
98
98
serviceName -> {
@@ -103,6 +103,26 @@ public ActiveSessionFactory() {
103
103
this .factories = ImmutableMap .copyOf (builder );
104
104
}
105
105
106
+ private void bind (
107
+ Map <Predicate <Capabilities >, SessionFactory > builder ,
108
+ String className ,
109
+ Predicate <Capabilities > predicate ,
110
+ Capabilities capabilities ) {
111
+ try {
112
+ Class <?> clazz = CLASS_EXISTS .apply (className );
113
+ if (clazz == null ) {
114
+ return ;
115
+ }
116
+
117
+ Class <? extends WebDriver > driverClass = clazz .asSubclass (WebDriver .class );
118
+ builder .put (
119
+ predicate ,
120
+ new InMemorySession .Factory (new DefaultDriverProvider (capabilities , driverClass )));
121
+ } catch (ClassCastException ignored ) {
122
+ // Just carry on. Everything is fine.
123
+ }
124
+ }
125
+
106
126
private static Predicate <Capabilities > browserName (Capabilities caps ) {
107
127
return toCompare -> caps .getBrowserName ().equals (toCompare .getBrowserName ());
108
128
}
0 commit comments