forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Grid custom capability mutators
Custom capability mutators(with ordering priority) can be placed as part of Grid setup and will be used. Fixes SeleniumHQ#13628
- Loading branch information
Showing
6 changed files
with
230 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
java/src/org/openqa/selenium/grid/node/config/CapabilitiesMutatorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
java/src/org/openqa/selenium/grid/node/config/CapabilityMutator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
java/test/org/openqa/selenium/grid/node/config/CapabilitiesMutatorServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |