17
17
18
18
package org .openqa .selenium .remote .server ;
19
19
20
+ import com .google .common .collect .ImmutableList ;
21
+
20
22
import org .openqa .selenium .Capabilities ;
21
23
import org .openqa .selenium .Platform ;
22
24
import org .openqa .selenium .WebDriver ;
25
27
26
28
import java .util .Collections ;
27
29
import java .util .HashMap ;
30
+ import java .util .List ;
28
31
import java .util .Map ;
29
32
import java .util .ServiceLoader ;
30
33
import java .util .Set ;
@@ -42,15 +45,23 @@ public class DefaultDriverSessions implements DriverSessions {
42
45
private final Map <SessionId , Session > sessionIdToDriver =
43
46
new ConcurrentHashMap <SessionId , Session >();
44
47
45
- private static Map <Capabilities , String > defaultDrivers = new HashMap <Capabilities , String >() {{
46
- put (DesiredCapabilities .chrome (), "org.openqa.selenium.chrome.ChromeDriver" );
47
- put (DesiredCapabilities .firefox (), "org.openqa.selenium.firefox.FirefoxDriver" );
48
- put (DesiredCapabilities .internetExplorer (), "org.openqa.selenium.ie.InternetExplorerDriver" );
49
- put (DesiredCapabilities .opera (), "com.opera.core.systems.OperaDriver" );
50
- put (DesiredCapabilities .operaBlink (), "org.openqa.selenium.opera.OperaDriver" );
51
- put (DesiredCapabilities .safari (), "org.openqa.selenium.safari.SafariDriver" );
52
- put (DesiredCapabilities .phantomjs (), "org.openqa.selenium.phantomjs.PhantomJSDriver" );
53
- }};
48
+ private static List <DriverProvider > defaultDriverProviders =
49
+ new ImmutableList .Builder <DriverProvider >()
50
+ .add (new DefaultDriverProvider (DesiredCapabilities .chrome (),
51
+ "org.openqa.selenium.chrome.ChromeDriver" ))
52
+ .add (new DefaultDriverProvider (DesiredCapabilities .firefox (),
53
+ "org.openqa.selenium.firefox.FirefoxDriver" ))
54
+ .add (new DefaultDriverProvider (DesiredCapabilities .internetExplorer (),
55
+ "org.openqa.selenium.ie.InternetExplorerDriver" ))
56
+ .add (new DefaultDriverProvider (DesiredCapabilities .opera (),
57
+ "com.opera.core.systems.OperaDriver" ))
58
+ .add (new DefaultDriverProvider (DesiredCapabilities .operaBlink (),
59
+ "org.openqa.selenium.opera.OperaDriver" ))
60
+ .add (new DefaultDriverProvider (DesiredCapabilities .safari (),
61
+ "org.openqa.selenium.safari.SafariDriver" ))
62
+ .add (new DefaultDriverProvider (DesiredCapabilities .phantomjs (),
63
+ "org.openqa.selenium.phantomjs.PhantomJSDriver" ))
64
+ .build ();
54
65
55
66
public DefaultDriverSessions () {
56
67
this (Platform .getCurrent (), new DefaultDriverFactory ());
@@ -67,42 +78,28 @@ public DefaultDriverSessions(
67
78
protected DefaultDriverSessions (Platform runningOn , DriverFactory factory ) {
68
79
this .factory = factory ;
69
80
registerDefaults (runningOn );
70
- registerDriverProviders (runningOn );
81
+ registerServiceLoaders (runningOn );
71
82
}
72
83
73
84
private void registerDefaults (Platform current ) {
74
- for (Map .Entry <Capabilities , String > entry : defaultDrivers .entrySet ()) {
75
- Capabilities caps = entry .getKey ();
76
- if (caps .getPlatform () == null || caps .getPlatform () == Platform .ANY || current .is (caps .getPlatform ())) {
77
- registerDriver (caps , entry .getValue ());
78
- } else {
79
- log .info ("Default driver " + entry .getValue () + " registration is skipped: registration capabilities "
80
- + caps .toString () + " does not match with current platform: " + current .toString ());
81
- }
85
+ for (DriverProvider provider : defaultDriverProviders ) {
86
+ registerDriverProvider (current , provider );
82
87
}
83
88
}
84
89
85
- private void registerDriverProviders (Platform current ) {
90
+ private void registerServiceLoaders (Platform current ) {
86
91
for (DriverProvider provider : ServiceLoader .load (DriverProvider .class )) {
87
- Capabilities caps = provider .getProvidedCapabilities ();
88
- if (caps .getPlatform () == null || caps .getPlatform () == Platform .ANY || current .is (caps .getPlatform ())) {
89
- factory .registerDriverProvider (caps , provider );
90
- } else {
91
- log .info ("Driver provider " + provider + " registration is skipped: registration capabilities "
92
- + caps .toString () + " does not match with current platform: " + current .toString ());
93
- }
92
+ registerDriverProvider (current , provider );
94
93
}
95
94
}
96
95
97
- private void registerDriver (Capabilities caps , String className ) {
98
- try {
99
- registerDriver (caps , Class .forName (className ).asSubclass (WebDriver .class ));
100
- } catch (ClassNotFoundException e ) {
101
- log .log (Level .INFO , "Unable to register driver with className " + className + " due to ClassNotFoundException" );
102
- } catch (NoClassDefFoundError e ) {
103
- log .log (Level .WARNING , "Unable to register driver with className " + className + " due to NoClassDefFoundError" );
104
- } catch (UnsupportedClassVersionError e ) {
105
- log .log (Level .WARNING , "Unable to register driver with className " + className + " due to UnsupportedClassVersionError" );
96
+ private void registerDriverProvider (Platform current , DriverProvider provider ) {
97
+ Capabilities caps = provider .getProvidedCapabilities ();
98
+ if (caps .getPlatform () == null || caps .getPlatform () == Platform .ANY || current .is (caps .getPlatform ())) {
99
+ factory .registerDriverProvider (caps , provider );
100
+ } else {
101
+ log .info ("Driver provider " + provider + " registration is skipped: registration capabilities "
102
+ + caps .toString () + " does not match with current platform: " + current .toString ());
106
103
}
107
104
}
108
105
0 commit comments