-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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<>(); | ||
|
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Selenium is using |
||
|
||
// Always apply this default capability mutator before applying any other mutator | ||
SessionCapabilitiesMutator defaultMutator = new SessionCapabilitiesMutator(stereotype); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The defaultMutator seems to be based off of |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
.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 |
---|---|---|
|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
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; | ||
} | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
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 aCopyOnWriteArrayList
? Can we not just useArrayList
instead ?