Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[grid] Support Grid custom capability mutators #13672

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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.Objects;
import java.util.ServiceLoader;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class CapabilitiesMutatorService {
Capabilities stereotype;
private final List<CapabilityMutator> customMutators = new CopyOnWriteArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the customMutators is expected to be populated as soon as the object gets created. After that Its only going to be iterated upon. So do we still need a CopyOnWriteArrayList? Can we not just use ArrayList instead ?


private static final Comparator<CapabilityMutator> MUTATOR_COMPARATOR =
Comparator.comparingInt(CapabilityMutator::getOrder).reversed();

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

loadAllCustomMutators();
}

public Capabilities getMutatedCapabilities(Capabilities desiredCapabilities) {
Objects.requireNonNull(desiredCapabilities, "desiredCapabilities must not be null");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Selenium is using Require.nonNull. So for consistency sake, maybe we can use that instead of Objects.requireNonNull


// Always apply this default capability mutator before applying any other mutator
SessionCapabilitiesMutator defaultMutator = new SessionCapabilitiesMutator(stereotype);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The defaultMutator seems to be based off of stereoType which is the same throughout the lifecycle of this object. So does it need to be created multiple times?

Capabilities newCapability = defaultMutator.apply(desiredCapabilities);

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

return newCapability;
}

private void loadAllCustomMutators() {
ServiceLoader<CapabilityMutator> loader = ServiceLoader.load(CapabilityMutator.class);

List<CapabilityMutator> allMutators = StreamSupport.stream(loader.spliterator(), false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if you could create a common method that internally takes care of building a stream, sorting the collection and returning back the list. This common method should be called via the loadAllCustomMutators and setCustomMutators. That way, the sorting part is also tested out.

.sorted(MUTATOR_COMPARATOR)
.collect(Collectors.toList());

customMutators.addAll(allMutators);
}

@VisibleForTesting
void setCustomMutators(List<CapabilityMutator> mutators) {
customMutators.clear();
customMutators.addAll(mutators);
}

@VisibleForTesting
List<CapabilityMutator> getCustomMutators() {
return customMutators;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
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.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please revert back this wildcard import?

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please help revert the wild card imports.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
};
}
}