Skip to content

Commit

Permalink
Add new session pipeline mutators for chrome and firefox Grid Nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Nov 2, 2017
1 parent 20f6931 commit 36342d0
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 0 deletions.
13 changes: 13 additions & 0 deletions java/server/src/org/openqa/grid/selenium/node/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# BUILD FILE SYNTAX: SKYLARK

java_library(
name = "node",
srcs = glob(["*.java"]),
deps = [
"//java/client/src/org/openqa/selenium/chrome:chrome",
"//java/client/src/org/openqa/selenium/firefox:firefox",
],
visibility = [
"//java/server/test/org/openqa/grid/selenium/node:",
],
)
66 changes: 66 additions & 0 deletions java/server/src/org/openqa/grid/selenium/node/ChromeMutator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.grid.selenium.node;

import static org.openqa.selenium.chrome.ChromeOptions.CAPABILITY;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

import org.openqa.selenium.ImmutableCapabilities;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class ChromeMutator implements Function<ImmutableCapabilities, ImmutableCapabilities> {

private final Object binary;

public ChromeMutator(Map<String, Object> config) {
if ("chrome".equals(config.get(BROWSER_NAME))) {
this.binary = config.get("chrome_binary");
} else {
this.binary = null;
}
}

@Override
public ImmutableCapabilities apply(ImmutableCapabilities capabilities) {
if (binary == null ||
!"chrome".equals(capabilities.getBrowserName())) {
return capabilities;
}

Map<String, Object> toReturn = new HashMap<>();
toReturn.putAll(capabilities.asMap());

Map<String, Object> options = new HashMap<>();
if (capabilities.getCapability(CAPABILITY) instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> asMap = (Map<String, Object>) capabilities.getCapability(CAPABILITY);
options.putAll(asMap);
}

if (!(options.get("binary") instanceof String)) {
options.put("binary", binary);
}

toReturn.put(CAPABILITY, options);

return new ImmutableCapabilities(toReturn);
}
}
83 changes: 83 additions & 0 deletions java/server/src/org/openqa/grid/selenium/node/FirefoxMutator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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.grid.selenium.node;

import static org.openqa.selenium.firefox.FirefoxDriver.BINARY;
import static org.openqa.selenium.firefox.FirefoxDriver.MARIONETTE;
import static org.openqa.selenium.firefox.FirefoxOptions.FIREFOX_OPTIONS;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

import org.openqa.selenium.ImmutableCapabilities;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class FirefoxMutator implements Function<ImmutableCapabilities, ImmutableCapabilities> {

private final Object binary;
private final Object marionette;

public FirefoxMutator(Map<String, Object> config) {
if ("firefox".equals(config.get(BROWSER_NAME))) {
this.binary = config.get(BINARY);
this.marionette = config.get(MARIONETTE);
} else {
this.binary = null;
this.marionette = null;
}
}

@Override
public ImmutableCapabilities apply(ImmutableCapabilities capabilities) {
if ((binary == null && marionette == null) ||
!"firefox".equals(capabilities.getBrowserName())) {
return capabilities;
}

Map<String, Object> options = new HashMap<>();
if (capabilities.getCapability(FIREFOX_OPTIONS) instanceof Map) {
//noinspection unchecked
Map<String, Object> originalOptions =
(Map<String, Object>) capabilities.getCapability(FIREFOX_OPTIONS);
options.putAll(originalOptions);
}

Map<String, Object> toReturn = new HashMap<>();
toReturn.putAll(capabilities.asMap());

if (binary != null) {
if (!(capabilities.getCapability(BINARY) instanceof String)) {
toReturn.put(BINARY, binary);
}
if (!(options.get("binary") instanceof String)) {
options.put("binary", binary);
}
}

if (marionette != null) {
toReturn.put(MARIONETTE, marionette);
}

if (!options.isEmpty()) {
toReturn.put(FIREFOX_OPTIONS, options);
}

return new ImmutableCapabilities(toReturn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openqa.grid.internal.listeners.TestSessionListener;
import org.openqa.grid.internal.listeners.TimeoutListener;
import org.openqa.grid.internal.utils.HtmlRenderer;
import org.openqa.grid.selenium.node.FirefoxMutator;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
Expand Down
14 changes: 14 additions & 0 deletions java/server/test/org/openqa/grid/selenium/node/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# BUILD FILE SYNTAX: SKYLARK

java_test(
name = "node",
srcs = glob(["*.java"]),
deps = [
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/chrome:chrome",
"//java/client/src/org/openqa/selenium/firefox:firefox",
"//java/server/src/org/openqa/grid/selenium/node:node",
"//third_party/java/junit:junit",
"//third_party/java/guava:guava",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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.grid.selenium.node;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.openqa.selenium.chrome.ChromeOptions.CAPABILITY;

import com.google.common.collect.ImmutableMap;

import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Map;

public class ChromeMutatorTest {

private final ImmutableCapabilities defaultConfig = new ImmutableCapabilities(
"browserName", "chrome",
"chrome_binary", "binary");
@Test
public void shouldDoNothingIfBrowserNameIsNotChrome() {
ImmutableCapabilities caps = new ImmutableCapabilities("browserName", "cake");

ImmutableCapabilities seen = new ChromeMutator(defaultConfig.asMap()).apply(caps);

// Make sure we return exactly the same instance of the capabilities, and not just a copy.
assertSame(caps, seen);
}

@Test
public void shouldDoNothingIfCapabilitiesUsedToConfigureMutatorAreNotChromeBased() {
ImmutableCapabilities config = new ImmutableCapabilities(
"browserName", "foo",
CAPABILITY, ImmutableMap.of("binary", "cake"));

ImmutableCapabilities caps = new ImmutableCapabilities("browserName", "chrome");
ImmutableCapabilities seen = new FirefoxMutator(config.asMap()).apply(caps);

assertSame(caps, seen);
}

@Test
public void shouldInjectBinaryIfNotSpecified() {
ImmutableCapabilities caps = new ImmutableCapabilities(new ChromeOptions());
ImmutableCapabilities seen = new ChromeMutator(defaultConfig.asMap()).apply(caps);

@SuppressWarnings("unchecked")
Map<String, Object> options = (Map<String, Object>) seen.getCapability(CAPABILITY);

assertEquals(
options.get("binary"),
defaultConfig.getCapability("chrome_binary"));
}
}
121 changes: 121 additions & 0 deletions java/server/test/org/openqa/grid/selenium/node/FirefoxMutatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 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.grid.selenium.node;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.openqa.selenium.firefox.FirefoxDriver.BINARY;
import static org.openqa.selenium.firefox.FirefoxDriver.MARIONETTE;
import static org.openqa.selenium.firefox.FirefoxOptions.FIREFOX_OPTIONS;

import com.google.common.collect.ImmutableMap;

import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;

import java.util.Map;

public class FirefoxMutatorTest {

private final ImmutableCapabilities defaultConfig = new ImmutableCapabilities(
"browserName", "firefox",
BINARY, "binary",
"firefox_profile", "profile",
MARIONETTE, true);

@Test
public void shouldDoNothingIfBrowserNameIsNotFirefox() {
ImmutableCapabilities caps = new ImmutableCapabilities("browserName", "chrome");

ImmutableCapabilities seen = new FirefoxMutator(defaultConfig.asMap()).apply(caps);

// Make sure we return exactly the same instance of the capabilities, and not just a copy.
assertSame(caps, seen);
}

@Test
public void shouldDoNothingIfCapabilitiesUsedToConfigureMutatorAreNotFirefoxBased() {
ImmutableCapabilities config = new ImmutableCapabilities(
"browserName", "foo",
"firefox_binary", "cake");

ImmutableCapabilities caps = new ImmutableCapabilities("browserName", "firefox");
ImmutableCapabilities seen = new FirefoxMutator(config.asMap()).apply(caps);

assertSame(caps, seen);
}

@Test
public void shouldInjectBinaryIfNotSpecified() {
ImmutableCapabilities caps = new ImmutableCapabilities("browserName", "firefox");
ImmutableCapabilities seen = new FirefoxMutator(defaultConfig.asMap()).apply(caps);

assertEquals(
seen.getCapability("firefox_binary"),
defaultConfig.getCapability("firefox_binary"));

@SuppressWarnings("unchecked")
Map<String, Object> options = (Map<String, Object>) seen.getCapability(FIREFOX_OPTIONS);

assertEquals(
options.get("binary"),
defaultConfig.getCapability("firefox_binary"));
}

@Test
public void shouldInjectBinaryIfLegacyOptionUnsetButGeckoDriverOptionSet() {
ImmutableCapabilities caps = new ImmutableCapabilities(
"browserName", "firefox",
BINARY, "cheese",
FIREFOX_OPTIONS, ImmutableMap.of());
ImmutableCapabilities seen = new FirefoxMutator(defaultConfig.asMap()).apply(caps);

assertEquals("cheese", seen.getCapability(BINARY));

@SuppressWarnings("unchecked")
Map<String, Object> options = (Map<String, Object>) seen.getCapability(FIREFOX_OPTIONS);

assertEquals(defaultConfig.getCapability(BINARY), options.get("binary"));
}

@Test
public void shouldInjectBinaryIfGeckoDriverOptionUnsetButLegacyOptionSet() {
ImmutableCapabilities caps = new ImmutableCapabilities(
"browserName", "firefox",
FIREFOX_OPTIONS, ImmutableMap.of("binary", "cheese"));
ImmutableCapabilities seen = new FirefoxMutator(defaultConfig.asMap()).apply(caps);

assertEquals(defaultConfig.getCapability(BINARY), seen.getCapability(BINARY));

@SuppressWarnings("unchecked")
Map<String, Object> options = (Map<String, Object>) seen.getCapability(FIREFOX_OPTIONS);

assertEquals("cheese", options.get("binary"));
}

@Test
public void shouldInjectMarionetteValueNoMatterWhat() {
ImmutableCapabilities caps = new ImmutableCapabilities(
"browserName", "firefox",
MARIONETTE, "cheese");
ImmutableCapabilities seen = new FirefoxMutator(defaultConfig.asMap()).apply(caps);

assertEquals(defaultConfig.getCapability(MARIONETTE), seen.getCapability(MARIONETTE));
}
}

0 comments on commit 36342d0

Please sign in to comment.