Skip to content

Commit

Permalink
Fixing DefaultDriverFactory to register IE and Safari driver provider…
Browse files Browse the repository at this point in the history
…s with platform specification, because IE can be instantiated on Windows only and Safari can be instantiated on Mac only. Also deleting deprecated methods.
  • Loading branch information
barancev committed Dec 17, 2017
1 parent 1468a65 commit 7efa40a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.openqa.selenium.remote.server;

import static com.google.common.base.Preconditions.checkState;
import static org.openqa.selenium.Platform.ANY;
import static org.openqa.selenium.Platform.MAC;
import static org.openqa.selenium.Platform.WINDOWS;
import static org.openqa.selenium.remote.BrowserType.CHROME;
import static org.openqa.selenium.remote.BrowserType.EDGE;
import static org.openqa.selenium.remote.BrowserType.FIREFOX;
Expand All @@ -28,6 +31,7 @@
import static org.openqa.selenium.remote.BrowserType.PHANTOMJS;
import static org.openqa.selenium.remote.BrowserType.SAFARI;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
import static org.openqa.selenium.remote.server.DefaultDriverProvider.createProvider;

import com.google.common.annotations.VisibleForTesting;
Expand All @@ -46,26 +50,32 @@
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import java.util.stream.Stream;

public class DefaultDriverFactory implements DriverFactory {

private static final Logger LOG = Logger.getLogger(DefaultDriverFactory.class.getName());

private static final List<DriverProvider> DEFAULT_DRIVER_PROVIDERS =
new ImmutableMap.Builder<String, String>()
.put(FIREFOX, "org.openqa.selenium.firefox.FirefoxDriver")
.put(CHROME, "org.openqa.selenium.chrome.ChromeDriver")
.put(IE, "org.openqa.selenium.ie.InternetExplorerDriver")
.put(EDGE, "org.openqa.selenium.edge.EdgeDriver")
.put(OPERA, "com.opera.core.systems.OperaDriver")
.put(OPERA_BLINK, "org.openqa.selenium.opera.OperaDriver")
.put(SAFARI, "org.openqa.selenium.safari.SafariDriver")
.put(PHANTOMJS, "org.openqa.selenium.phantomjs.PhantomJSDriver")
.put(HTMLUNIT, "org.openqa.selenium.htmlunit.HtmlUnitDriver")
.build().entrySet().stream()
.map(e -> createProvider(new ImmutableCapabilities(BROWSER_NAME, e.getKey()), e.getValue()))
.filter(Objects::nonNull)
.collect(ImmutableList.toImmutableList());
Stream.concat(
new ImmutableMap.Builder<String, String>()
.put(FIREFOX, "org.openqa.selenium.firefox.FirefoxDriver")
.put(CHROME, "org.openqa.selenium.chrome.ChromeDriver")
.put(OPERA, "com.opera.core.systems.OperaDriver")
.put(OPERA_BLINK, "org.openqa.selenium.opera.OperaDriver")
.put(PHANTOMJS, "org.openqa.selenium.phantomjs.PhantomJSDriver")
.put(HTMLUNIT, "org.openqa.selenium.htmlunit.HtmlUnitDriver")
.build().entrySet().stream()
.map(e -> createProvider(new ImmutableCapabilities(BROWSER_NAME, e.getKey()), e.getValue()))
.filter(Objects::nonNull),
Stream.of(
createProvider(new ImmutableCapabilities(BROWSER_NAME, IE, PLATFORM_NAME, WINDOWS),
"org.openqa.selenium.ie.InternetExplorerDriver"),
createProvider(new ImmutableCapabilities(BROWSER_NAME, EDGE, PLATFORM_NAME, WINDOWS),
"org.openqa.selenium.edge.EdgeDriver"),
createProvider(new ImmutableCapabilities(BROWSER_NAME, SAFARI, PLATFORM_NAME, MAC),
"org.openqa.selenium.safari.SafariDriver"))
).collect(ImmutableList.toImmutableList());

private final Map<Capabilities, DriverProvider> capabilitiesToDriverProvider =
new ConcurrentHashMap<>();
Expand Down Expand Up @@ -99,10 +109,6 @@ public WebDriver newInstance(Capabilities capabilities) {
provider, capabilities));
}

public boolean hasMappingFor(Capabilities capabilities) {
return capabilitiesToDriverProvider.containsKey(capabilities);
}

private void registerDefaults(Platform current) {
for (DriverProvider provider : DEFAULT_DRIVER_PROVIDERS) {
registerDriverProvider(current, provider);
Expand Down Expand Up @@ -130,7 +136,7 @@ private void registerDriverProvider(Platform current, DriverProvider provider) {

private boolean platformMatches(Platform current, Capabilities caps) {
return caps.getPlatform() == null
|| caps.getPlatform() == Platform.ANY
|| caps.getPlatform() == ANY
|| current.is(caps.getPlatform());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,4 @@ public interface DriverFactory {
void registerDriverProvider(DriverProvider implementation);

WebDriver newInstance(Capabilities capabilities);

/**
* @deprecated This method is used in tests only and subject to be removed from the interface
* @param capabilities capabilities map
* @return true if the driver factory can provide a driver for the capabilities
*/
@Deprecated
boolean hasMappingFor(Capabilities capabilities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.openqa.selenium.remote.BrowserType.CHROME;
import static org.openqa.selenium.remote.BrowserType.FIREFOX;
import static org.openqa.selenium.remote.BrowserType.IE;
import static org.openqa.selenium.remote.BrowserType.SAFARI;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.StubDriver;
import org.openqa.selenium.remote.BrowserType;
Expand All @@ -40,84 +47,95 @@ public void setUp() throws Exception {
factory = new DefaultDriverFactory(Platform.getCurrent());
}

@Test
public void testShouldRegisterCorrectDefaultsOnMac() {
DefaultDriverFactory factory = new DefaultDriverFactory(Platform.MAC);

assertTrue(canInstantiate(factory, CHROME));
assertTrue(canInstantiate(factory, FIREFOX));
assertTrue(canInstantiate(factory, SAFARI));
assertFalse(canInstantiate(factory, IE));
}

@Test
public void testShouldRegisterCorrectDefaultsOnLinux() {
DefaultDriverFactory factory = new DefaultDriverFactory(Platform.LINUX);

assertTrue(canInstantiate(factory, CHROME));
assertTrue(canInstantiate(factory, FIREFOX));
assertFalse(canInstantiate(factory, SAFARI));
assertFalse(canInstantiate(factory, IE));
}

@Test
public void testShouldRegisterCorrectDefaultsOnWindows() {
DefaultDriverFactory factory = new DefaultDriverFactory(Platform.VISTA);

assertTrue(canInstantiate(factory, CHROME));
assertTrue(canInstantiate(factory, FIREFOX));
assertFalse(canInstantiate(factory, SAFARI));
assertTrue(canInstantiate(factory, IE));
}

private boolean canInstantiate(DefaultDriverFactory factory, String browser) {
Capabilities capabilities = new ImmutableCapabilities(BROWSER_NAME, browser);
return factory.getProviderMatching(capabilities).canCreateDriverInstanceFor(capabilities);
}

@Test
public void testShouldBeAbleToRegisterNewDrivers() {
DesiredCapabilities capabilities = new DesiredCapabilities("cheese", null, Platform.ANY);
assertFalse(factory.hasMappingFor(capabilities));
Capabilities capabilities = new DesiredCapabilities("cheese", null, Platform.ANY);
assertNotEquals(factory.getProviderMatching(capabilities).getProvidedCapabilities(), capabilities);

factory.registerDriverProvider(mockDriverProviderFor(capabilities));

assertTrue(factory.hasMappingFor(capabilities));
assertEquals(factory.getProviderMatching(capabilities).getProvidedCapabilities(), capabilities);
}

@Test
public void testShouldReturnMatchIfOneFieldMatchesAndOnlyOneDriverIsRegistered() {
DesiredCapabilities template = new DesiredCapabilities();
template.setBrowserName("foo");
template.setVersion("1.0");
template.setPlatform(Platform.getCurrent());

Capabilities template = new DesiredCapabilities("foo", "1.0", Platform.getCurrent());
DriverProvider provider = mockDriverProviderFor(template);

factory.registerDriverProvider(provider);

DesiredCapabilities example = new DesiredCapabilities();
example.setBrowserName(template.getBrowserName());

Capabilities example = new ImmutableCapabilities(BROWSER_NAME, template.getBrowserName());
assertEquals(provider, factory.getProviderMatching(example));
}

@Test
public void testShouldReturnDriverWhereTheMostCapabilitiesMatch() {
DesiredCapabilities first = new DesiredCapabilities();
first.setBrowserName("foo");
first.setVersion("1.0");

DesiredCapabilities second = new DesiredCapabilities();
second.setBrowserName("bar"); // Different name
second.setVersion("1.0");
Capabilities first = new DesiredCapabilities("foo", "1.0", Platform.ANY);
Capabilities second = new DesiredCapabilities("bar", "1.0", Platform.ANY);

DriverProvider provider1 = mockDriverProviderFor(first);
DriverProvider provider2 = mockDriverProviderFor(second);

factory.registerDriverProvider(provider1);
factory.registerDriverProvider(provider2);

DesiredCapabilities example = new DesiredCapabilities();

example.setBrowserName("foo");
Capabilities example = new ImmutableCapabilities(BROWSER_NAME, "foo");
assertEquals(provider1, factory.getProviderMatching(example));

example.setBrowserName("bar");
example = new ImmutableCapabilities(BROWSER_NAME, "bar");
assertEquals(provider2, factory.getProviderMatching(example));
}

@Test
public void testShouldReturnDriverWhereTheMostCapabilitiesMatch_lotsOfRegisteredDrivers() {
DriverProvider chromeProvider = mockDriverProviderFor(DesiredCapabilities.chrome());
DriverProvider firefoxProvider = mockDriverProviderFor(DesiredCapabilities.firefox());
DriverProvider htmlUnitProvider = mockDriverProviderFor(DesiredCapabilities.htmlUnit());
DriverProvider ieProvider = mockDriverProviderFor(DesiredCapabilities.internetExplorer());
DriverProvider operaProvider = mockDriverProviderFor(DesiredCapabilities.operaBlink());

factory.registerDriverProvider(chromeProvider);
factory.registerDriverProvider(firefoxProvider);
factory.registerDriverProvider(htmlUnitProvider);
factory.registerDriverProvider(ieProvider);
factory.registerDriverProvider(operaProvider);

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setBrowserName(BrowserType.IE);
desiredCapabilities.setBrowserName(IE);
desiredCapabilities.setVersion("");
desiredCapabilities.setJavascriptEnabled(true);
desiredCapabilities.setPlatform(Platform.ANY);

assertEquals(ieProvider, factory.getProviderMatching(desiredCapabilities));
assertEquals(IE,
factory.getProviderMatching(desiredCapabilities).getProvidedCapabilities().getBrowserName());
}

@Test
public void testShouldReturnMostRecentlyAddedDriverWhenAllCapabilitiesAreEqual() {
Capabilities capabilities = DesiredCapabilities.firefox();
Capabilities capabilities = new ImmutableCapabilities(BROWSER_NAME, "cheese");

DriverProvider provider1 = mockDriverProviderFor(capabilities);
DriverProvider provider2 = mockDriverProviderFor(capabilities);
Expand Down Expand Up @@ -178,8 +196,7 @@ public void testShouldConsiderJavascriptCapabilities() {

@Test
public void testShouldCallAConstructorTakingACapabilitiesArgInPreferenceToANoArgOne() {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("example");
Capabilities caps = new ImmutableCapabilities(BROWSER_NAME, "example");
factory.registerDriverProvider(new DefaultDriverProvider(caps, CapabilitiesDriver.class));

CapabilitiesDriver driver = (CapabilitiesDriver) factory.newInstance(caps);
Expand All @@ -204,10 +221,10 @@ public Capabilities getCapabilities() {
}
}

private DriverProvider mockDriverProviderFor(Capabilities nojavascript) {
DriverProvider nojavascriptProvider = mock(DriverProvider.class);
when(nojavascriptProvider.getProvidedCapabilities()).thenReturn(nojavascript);
return nojavascriptProvider;
private DriverProvider mockDriverProviderFor(Capabilities capabilities) {
DriverProvider provider = mock(DriverProvider.class);
when(provider.getProvidedCapabilities()).thenReturn(capabilities);
return provider;
}

}

0 comments on commit 7efa40a

Please sign in to comment.