|
20 | 20 |
|
21 | 21 | import org.openqa.selenium.Capabilities;
|
22 | 22 | import org.openqa.selenium.WebDriver;
|
23 |
| -import org.openqa.selenium.WebDriverException; |
24 | 23 |
|
25 |
| -import java.lang.reflect.Constructor; |
26 |
| -import java.lang.reflect.InvocationTargetException; |
27 | 24 | import java.util.Map;
|
28 | 25 | import java.util.concurrent.ConcurrentHashMap;
|
29 |
| -import java.util.logging.Logger; |
30 | 26 |
|
31 | 27 | public class DefaultDriverFactory implements DriverFactory {
|
32 | 28 |
|
33 |
| - private static final Logger log = Logger.getLogger(DefaultDriverFactory.class.getName()); |
34 |
| - |
35 |
| - private Map<Capabilities, Class<? extends WebDriver>> capabilitiesToDriver = |
36 |
| - new ConcurrentHashMap<Capabilities, Class<? extends WebDriver>>(); |
| 29 | + private Map<Capabilities, DriverProvider> capabilitiesToDriverProvider = |
| 30 | + new ConcurrentHashMap<Capabilities, DriverProvider>(); |
37 | 31 |
|
38 | 32 | public void registerDriver(Capabilities capabilities, Class<? extends WebDriver> implementation) {
|
39 |
| - capabilitiesToDriver.put(capabilities, implementation); |
| 33 | + capabilitiesToDriverProvider.put(capabilities, |
| 34 | + new DefaultDriverProvider(capabilities, implementation)); |
40 | 35 | }
|
41 | 36 |
|
42 | 37 | protected Class<? extends WebDriver> getBestMatchFor(Capabilities desired) {
|
| 38 | + return getProviderMatching(desired).getDriverClass(); |
| 39 | + } |
| 40 | + |
| 41 | + protected DriverProvider getProviderMatching(Capabilities desired) { |
43 | 42 | // We won't be able to make a match if no drivers have been registered.
|
44 |
| - checkState(!capabilitiesToDriver.isEmpty(), |
45 |
| - "No drivers have been registered, will be unable to match %s", desired); |
| 43 | + checkState(!capabilitiesToDriverProvider.isEmpty(), |
| 44 | + "No drivers have been registered, will be unable to match %s", desired); |
46 | 45 | Capabilities bestMatchingCapabilities =
|
47 |
| - CapabilitiesComparator.getBestMatch(desired, capabilitiesToDriver.keySet()); |
48 |
| - return capabilitiesToDriver.get(bestMatchingCapabilities); |
| 46 | + CapabilitiesComparator.getBestMatch(desired, capabilitiesToDriverProvider.keySet()); |
| 47 | + return capabilitiesToDriverProvider.get(bestMatchingCapabilities); |
49 | 48 | }
|
50 | 49 |
|
51 | 50 | public WebDriver newInstance(Capabilities capabilities) {
|
52 |
| - log.info("Creating a new session for " + capabilities); |
53 |
| - Class<? extends WebDriver> clazz = getBestMatchFor(capabilities); |
54 |
| - |
55 |
| - // Try and call the single arg constructor that takes a capabilities first |
56 |
| - return callConstructor(clazz, capabilities); |
57 |
| - } |
58 |
| - |
59 |
| - private WebDriver callConstructor(Class<? extends WebDriver> from, Capabilities capabilities) { |
60 |
| - try { |
61 |
| - Constructor<? extends WebDriver> constructor = from.getConstructor(Capabilities.class); |
62 |
| - return constructor.newInstance(capabilities); |
63 |
| - } catch (NoSuchMethodException e) { |
64 |
| - try { |
65 |
| - return from.newInstance(); |
66 |
| - } catch (InstantiationException e1) { |
67 |
| - throw new WebDriverException(e); |
68 |
| - } catch (IllegalAccessException e1) { |
69 |
| - throw new WebDriverException(e); |
70 |
| - } |
71 |
| - } catch (InvocationTargetException e) { |
72 |
| - throw new WebDriverException(e); |
73 |
| - } catch (InstantiationException e) { |
74 |
| - throw new WebDriverException(e); |
75 |
| - } catch (IllegalAccessException e) { |
76 |
| - throw new WebDriverException(e); |
77 |
| - } |
| 51 | + return getProviderMatching(capabilities).newInstance(capabilities); |
78 | 52 | }
|
79 | 53 |
|
80 | 54 | public boolean hasMappingFor(Capabilities capabilities) {
|
81 |
| - return capabilitiesToDriver.containsKey(capabilities); |
| 55 | + return capabilitiesToDriverProvider.containsKey(capabilities); |
82 | 56 | }
|
83 | 57 | }
|
0 commit comments