Skip to content

Commit

Permalink
Implement ability to load driver providers using ServiceLoader. A use…
Browse files Browse the repository at this point in the history
…r can add new providers or override existing ones. To use this ability a user should:

1) Create one or several classes that implement DriverProvider interface and have no-args constructor. The method getProvidedCapabilities should return capabilities to match against. The method newInstance should implement custom driver creation logic.
2) Create a file META-INF/services/org.openqa.selenium.remote.server.DriverProvider
and list your classes in this file (one full class name per line).
3) Start selenium server (or a node grid) with the classpath that includes your custom classes and META-INF directory that lists them.
4) Starting a node set -browser option with the parameters that correspont the capabilities returned by getProvidedCapabilities method of a provider.
Also I'm going to add a wiki page that describes this new feature in more details.
  • Loading branch information
barancev committed May 10, 2014
1 parent d8eff0c commit 704e160
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public class DefaultDriverFactory implements DriverFactory {
new ConcurrentHashMap<Capabilities, DriverProvider>();

public void registerDriver(Capabilities capabilities, Class<? extends WebDriver> implementation) {
capabilitiesToDriverProvider.put(capabilities,
new DefaultDriverProvider(capabilities, implementation));
registerDriverProvider(capabilities, new DefaultDriverProvider(capabilities, implementation));
}

public void registerDriverProvider(Capabilities capabilities, DriverProvider implementation) {
capabilitiesToDriverProvider.put(capabilities, implementation);
}

protected Class<? extends WebDriver> getBestMatchFor(Capabilities desired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -65,6 +66,7 @@ public DefaultDriverSessions(
protected DefaultDriverSessions(Platform runningOn, DriverFactory factory) {
this.factory = factory;
registerDefaults(runningOn);
registerDriverProviders(runningOn);
}

private void registerDefaults(Platform current) {
Expand All @@ -81,6 +83,18 @@ private void registerDefaults(Platform current) {
}
}

private void registerDriverProviders(Platform current) {
for (DriverProvider provider : ServiceLoader.load(DriverProvider.class)) {
Capabilities caps = provider.getProvidedCapabilities();
if (caps.getPlatform() == null || caps.getPlatform().is(current)) {
factory.registerDriverProvider(caps, provider);
} else {
log.info("Driver provider " + provider + " registration is skipped: registration capabilities "
+ caps.toString() + " does not match with current platform: " + current.toString());
}
}
}

private void registerDriver(Capabilities caps, String className) {
try {
registerDriver(caps, Class.forName(className).asSubclass(WebDriver.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
public interface DriverFactory {
void registerDriver(Capabilities capabilities, Class<? extends WebDriver> implementation);

void registerDriverProvider(Capabilities capabilities, DriverProvider implementation);

WebDriver newInstance(Capabilities capabilities);

boolean hasMappingFor(Capabilities capabilities);
Expand Down

0 comments on commit 704e160

Please sign in to comment.