Skip to content

Commit

Permalink
Support Grid custom capability mutators
Browse files Browse the repository at this point in the history
Custom capability mutators(with ordering priority) can be placed as part of Grid setup and will be used.

Fixes SeleniumHQ#13628
  • Loading branch information
Purus committed Mar 10, 2024
1 parent 7384157 commit 124a3cc
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 47 deletions.
Expand Up @@ -9,6 +9,7 @@ java_library(
],
deps = [
"//java:auto-service",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/grid/config",
"//java/src/org/openqa/selenium/grid/data",
"//java/src/org/openqa/selenium/grid/distributor",
Expand Down
@@ -0,0 +1,64 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.node.config;

import com.google.common.annotations.VisibleForTesting;
import org.openqa.selenium.Capabilities;

import java.util.Comparator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class CapabilitiesMutatorService {
Capabilities stereotype;
private List<CapabilityMutator> customMutators;

public CapabilitiesMutatorService(Capabilities stereotype) {
this.stereotype = stereotype;

ServiceLoader<CapabilityMutator> loader = ServiceLoader.load(CapabilityMutator.class);

customMutators = StreamSupport.stream(loader.spliterator(), false)
.sorted(Comparator.comparingInt(CapabilityMutator::getOrder).reversed())
.collect(Collectors.toList());
}

public Capabilities getMutatedCapabilities(Capabilities desiredCapabilities) {
// Always apply this default capability mutator before applying any other mutator
SessionCapabilitiesMutator defaultMutator = new SessionCapabilitiesMutator(stereotype);
Capabilities newCapability = defaultMutator.apply(desiredCapabilities);

for (CapabilityMutator customMutator : customMutators) {
newCapability = customMutator.apply(newCapability);
}

return newCapability;
}

@VisibleForTesting
void setCustomMutators(List<CapabilityMutator> mutators) {
customMutators = mutators;
}

@VisibleForTesting
List<CapabilityMutator> getCustomMutators() {
return customMutators;
}
}
@@ -0,0 +1,29 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.node.config;

import org.openqa.selenium.Capabilities;

import java.util.function.Function;

public interface CapabilityMutator extends Function<Capabilities, Capabilities> {

default int getOrder() {
return 100;
}
}
Expand Up @@ -17,32 +17,7 @@

package org.openqa.selenium.grid.node.config;

import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT;
import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.PersistentCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.*;
import org.openqa.selenium.devtools.CdpEndpointFinder;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.node.ActiveSession;
Expand All @@ -53,21 +28,27 @@
import org.openqa.selenium.manager.SeleniumManagerOutput.Result;
import org.openqa.selenium.net.HostIdentifier;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ProtocolHandshake;
import org.openqa.selenium.remote.Response;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.service.DriverFinder;
import org.openqa.selenium.remote.service.DriverService;
import org.openqa.selenium.remote.tracing.AttributeKey;
import org.openqa.selenium.remote.tracing.AttributeMap;
import org.openqa.selenium.remote.tracing.Span;
import org.openqa.selenium.remote.tracing.Status;
import org.openqa.selenium.remote.tracing.Tracer;
import org.openqa.selenium.remote.tracing.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Stream;

import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT;
import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;

public class DriverServiceSessionFactory implements SessionFactory {

Expand All @@ -79,7 +60,7 @@ public class DriverServiceSessionFactory implements SessionFactory {
private final Predicate<Capabilities> predicate;
private final DriverService.Builder<?, ?> builder;
private final Capabilities stereotype;
private final SessionCapabilitiesMutator sessionCapabilitiesMutator;
private final CapabilitiesMutatorService capabilitiesMutatorService;

public DriverServiceSessionFactory(
Tracer tracer,
Expand All @@ -94,7 +75,7 @@ public DriverServiceSessionFactory(
this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));
this.predicate = Require.nonNull("Accepted capabilities predicate", predicate);
this.builder = Require.nonNull("Driver service builder", builder);
this.sessionCapabilitiesMutator = new SessionCapabilitiesMutator(this.stereotype);
this.capabilitiesMutatorService = new CapabilitiesMutatorService(this.stereotype);
}

@Override
Expand Down Expand Up @@ -124,7 +105,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
try {

Capabilities capabilities =
sessionCapabilitiesMutator.apply(sessionRequest.getDesiredCapabilities());
capabilitiesMutatorService.getMutatedCapabilities(sessionRequest.getDesiredCapabilities());

CAPABILITIES.accept(span, capabilities);
CAPABILITIES_EVENT.accept(attributeMap, capabilities);
Expand Down
Expand Up @@ -18,17 +18,13 @@
package org.openqa.selenium.grid.node.config;

import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.PersistentCapabilities;

public class SessionCapabilitiesMutator implements Function<Capabilities, Capabilities> {
import java.util.*;

public class SessionCapabilitiesMutator implements CapabilityMutator {

private static final ImmutableMap<String, String> BROWSER_OPTIONS =
ImmutableMap.of(
Expand Down
@@ -0,0 +1,112 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.node.config;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.PersistentCapabilities;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;

public class CapabilitiesMutatorServiceTest {

@Test
void applyDefaultCapabilityMutator() {
CapabilitiesMutatorService mutatorService = new CapabilitiesMutatorService(getStereotype());
Map<String, Object> newCapabilities = mutatorService.getMutatedCapabilities(getRequirdCapabilities()).asMap();

assertThat(mutatorService.getCustomMutators().size()).isEqualTo(0);

assertThat(newCapabilities.get("browserName")).isEqualTo("chrome");

assertThat(newCapabilities)
.extractingByKey("goog:chromeOptions")
.asInstanceOf(MAP)
.extractingByKey("args")
.asInstanceOf(LIST)
.contains("incognito", "window-size=500,500");
}

@Test
void applyCustomCapabilityMutator() {
CapabilitiesMutatorService mutatorService = new CapabilitiesMutatorService(getStereotype());
mutatorService.setCustomMutators(List.of(getTestMutatorA()));

Capabilities newCapabilities = mutatorService.getMutatedCapabilities(getRequirdCapabilities());

assertThat(mutatorService.getCustomMutators().size()).isEqualTo(1);

assertThat(newCapabilities.getCapability("browserName")).isEqualTo("chrome");
assertThat(newCapabilities.getCapability("key")).isEqualTo("foo");
}

@Test
void applyCustomCapabilityMutator_with_order() {
CapabilitiesMutatorService mutatorService = new CapabilitiesMutatorService(getStereotype());
mutatorService.setCustomMutators(List.of(getTestHighOrderMutator(), getTestMutatorA()));

Capabilities newCapabilities = mutatorService.getMutatedCapabilities(getRequirdCapabilities());

assertThat(mutatorService.getCustomMutators().size()).isEqualTo(2);

assertThat(newCapabilities.getCapability("browserName")).isEqualTo("chrome");
assertThat(newCapabilities.getCapability("key")).isEqualTo("foo");
assertThat(newCapabilities.getCapability("someKey")).isEqualTo("someValue");
}

private Capabilities getStereotype() {
return new ImmutableCapabilities("browserName", "chrome");
}

private Capabilities getRequirdCapabilities() {
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("args", Arrays.asList("incognito", "window-size=500,500"));

return new ImmutableCapabilities(
"browserName", "chrome",
"goog:chromeOptions", chromeOptions);
}

private CapabilityMutator getTestMutatorA() {
return capabilities -> new PersistentCapabilities(capabilities).setCapability("key", "foo");
}

private CapabilityMutator getTestHighOrderMutator() {
return new CapabilityMutator() {
@Override
public Capabilities apply(Capabilities capabilities) {
return new PersistentCapabilities(capabilities)
.setCapability("key", "bar")
.setCapability("someKey", "someValue");
}

@Override
public int getOrder() {
return 10;
}
};
}
}

0 comments on commit 124a3cc

Please sign in to comment.