Skip to content

Commit

Permalink
ActiveSession: allow new factories to be bound
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Aug 27, 2017
1 parent 6c37bb4 commit 1da22d4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ActiveSessionFactory {
}
};

private final Map<Predicate<Capabilities>, SessionFactory> factories;
private volatile Map<Predicate<Capabilities>, SessionFactory> factories;

public ActiveSessionFactory() {
// Insertion order matters. The first matching predicate is always used for matching.
Expand Down Expand Up @@ -104,6 +104,23 @@ public ActiveSessionFactory() {
this.factories = ImmutableMap.copyOf(builder);
}

public synchronized ActiveSessionFactory bind(
Predicate<Capabilities> onThis,
SessionFactory useThis) {
Objects.requireNonNull(onThis, "Predicated needed.");
Objects.requireNonNull(useThis, "SessionFactory is required");

LOG.info(String.format("Binding %s to respond to %s", useThis, onThis));

LinkedHashMap<Predicate<Capabilities>, SessionFactory> newMap = new LinkedHashMap<>();
newMap.put(onThis, useThis);
newMap.putAll(factories);

factories = newMap;

return this;
}

@VisibleForTesting
protected Iterable<DriverProvider> loadDriverProviders() {
return () -> ServiceLoader.load(DriverProvider.class).iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ public void deleteSession(SessionId sessionId) {
}

@Override
public void registerDriver(Capabilities capabilities,
Class<? extends WebDriver> implementation) {
public void registerDriver(
Capabilities capabilities,
Class<? extends WebDriver> implementation) {
throw new UnsupportedOperationException("registerDriver");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.remote.server;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -46,17 +47,31 @@ protected Iterable<DriverProvider> loadDriverProviders() {
}
};

try (NewSessionPayload payload = new NewSessionPayload(toPayload(caps))) {
try (NewSessionPayload payload = new NewSessionPayload(toPayload(caps.getBrowserName()))) {
ActiveSession session = sessionFactory.createSession(payload);
assertEquals(driver, session.getWrappedDriver());
}
}

private Map<String, Object> toPayload(Capabilities caps) {
@Test
public void canBindNewFactoriesAtRunTime() throws IOException {
ActiveSession session = Mockito.mock(ActiveSession.class);

ActiveSessionFactory sessionFactory = new ActiveSessionFactory()
.bind(caps -> "cheese".equals(caps.getBrowserName()), payload -> session);

try (NewSessionPayload payload = new NewSessionPayload(toPayload("cheese"))) {
ActiveSession created = sessionFactory.createSession(payload);

assertSame(session, created);
}
}

private Map<String, Object> toPayload(String browserName) {
return ImmutableMap.of(
"capabilities", ImmutableMap.of(
"alwaysMatch", caps.asMap()),
"desiredCapabilities", caps.asMap());
"alwaysMatch", ImmutableMap.of("browserName", browserName)),
"desiredCapabilities", ImmutableMap.of("browserName", browserName));
}

private static class StubbedProvider implements DriverProvider {
Expand Down

0 comments on commit 1da22d4

Please sign in to comment.