Skip to content

Commit 42b8f6b

Browse files
committedJun 8, 2015
server: Adding a guard that prevents starting "IE instead of Opera" (or some other unwanted browser that obviously does not match the desired capabilities). Previously if a new session request is forwarded to a node it results in a driver instance creation in any case. For example, let's suppose a user starts a node with -browser browserName=opera option, and there is no operadriver in the classpath. Then the user requests a new remote driver with browserName=opera. The hub forwards the request to the node, and the node attempts to find the "best matching" driver provider. As far as opera is not available, it can start any other browser. Because "best matching" does not imply matching. The new guard prevents this unwanted behavior.
1 parent ed4a89e commit 42b8f6b

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed
 

‎java/server/src/org/openqa/selenium/remote/server/DefaultDriverFactory.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.openqa.selenium.Capabilities;
2525
import org.openqa.selenium.WebDriver;
26+
import org.openqa.selenium.WebDriverException;
2627

2728
import java.util.Map;
2829
import java.util.concurrent.ConcurrentHashMap;
@@ -54,7 +55,14 @@ DriverProvider getProviderMatching(Capabilities desired) {
5455
}
5556

5657
public WebDriver newInstance(Capabilities capabilities) {
57-
return getProviderMatching(capabilities).newInstance(capabilities);
58+
DriverProvider provider = getProviderMatching(capabilities);
59+
if (provider.canCreateDriverInstanceFor(capabilities)) {
60+
return getProviderMatching(capabilities).newInstance(capabilities);
61+
} else {
62+
throw new WebDriverException(String.format(
63+
"The best matching driver provider %s can't create a new driver instance for %s",
64+
provider, capabilities));
65+
}
5866
}
5967

6068
public boolean hasMappingFor(Capabilities capabilities) {

‎java/server/src/org/openqa/selenium/remote/server/DefaultDriverProvider.java

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
import java.util.logging.Level;
2626
import java.util.logging.Logger;
2727

28+
/**
29+
* This driver provider uses reflection to find and call a driver constructor that accepts
30+
* a parameter of Capabilities type.
31+
*/
2832
public class DefaultDriverProvider implements DriverProvider {
2933

3034
private static final Logger LOG = Logger.getLogger(DefaultDriverProvider.class.getName());
@@ -48,11 +52,25 @@ public Capabilities getProvidedCapabilities() {
4852
return capabilities;
4953
}
5054

55+
/**
56+
* Checks that driver class can be loaded.
57+
*/
5158
@Override
5259
public boolean canCreateDriverInstances() {
5360
return getDriverClass() != null;
5461
}
5562

63+
/**
64+
* Checks that the browser name set in the provided capabilities matches the browser name
65+
* set in the desired capabilities.
66+
* @param capabilities The desired capabilities
67+
* @return true if the browser name is the same, false otherwise
68+
*/
69+
@Override
70+
public boolean canCreateDriverInstanceFor(Capabilities capabilities) {
71+
return this.capabilities.getBrowserName().equals(capabilities.getBrowserName());
72+
}
73+
5674
private Class<? extends WebDriver> getDriverClass() {
5775
if (driverClass != null) {
5876
return driverClass;

‎java/server/src/org/openqa/selenium/remote/server/DriverProvider.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ public interface DriverProvider {
4444
Capabilities getProvidedCapabilities();
4545

4646
/**
47-
* Checks if the provider can create driver instances.
47+
* Checks if the provider can create driver instances "in general".
4848
*
4949
* @return true if the provider can create driver instances.
5050
*/
5151
boolean canCreateDriverInstances();
5252

53+
/**
54+
* Checks if the provider can create driver instance with the desired capabilities.
55+
*
56+
* @return true if the provider can create driver instance with the desired capabilities.
57+
*/
58+
boolean canCreateDriverInstanceFor(Capabilities capabilities);
59+
5360
/**
5461
* Creates a new driver instance. The specified capabilities are to be passed to the driver
5562
* constructor.

0 commit comments

Comments
 (0)