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

[java] Merge capabilities of slot with the new session request capabilities #11369

Merged
merged 11 commits into from
Jan 30, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.PersistentCapabilities;

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;
Expand Down Expand Up @@ -56,33 +57,137 @@ public Capabilities apply(Capabilities capabilities) {
}

String browserName = capabilities.getBrowserName().toLowerCase();
if (!BROWSER_OPTIONS.containsKey(browserName)) {
return capabilities;
}

String options = BROWSER_OPTIONS.get(browserName);
if (!slotStereotype.asMap().containsKey(options)) {
return capabilities;
if (slotStereotype.asMap().containsKey(options) && capabilities.asMap().containsKey(options)) {

@SuppressWarnings("unchecked")
Map<String, Object>
stereotypeOptions =
(Map<String, Object>) slotStereotype.asMap().get(options);

@SuppressWarnings("unchecked")
Map<String, Object> capsOptions = (Map<String, Object>) capabilities.asMap().get(options);

// Merge top level capabilities, excluding browser specific options.
// This will overwrite the browser options too, but it does not matter since we tackle it separately just after this.
Map<String, Object> toReturn = new HashMap<>(slotStereotype.merge(capabilities).asMap());

// Merge browser specific stereotype and capabilities options
switch (browserName) {
case "chrome":
case "microsoftedge":
diemol marked this conversation as resolved.
Show resolved Hide resolved
toReturn.put(options, mergeChromiumOptions(stereotypeOptions, capsOptions));
break;
case "firefox":
toReturn.put(options, mergeFirefoxOptions(stereotypeOptions, capsOptions));
break;
default:
break;
}

return new ImmutableCapabilities(toReturn);
}

@SuppressWarnings("unchecked")
Map<String, Object> stereotypeOptions = (Map<String, Object>) slotStereotype.asMap().get(options);
return slotStereotype.merge(capabilities);
}

private Map<String, Object> mergeChromiumOptions(Map<String, Object> stereotypeOptions,
Map<String, Object> capsOptions) {
Map<String, Object> toReturn = new HashMap<>(stereotypeOptions);

Map<String, Object> toReturn = new HashMap<>(capabilities.asMap());
for (String name : capsOptions.keySet()) {
if (name.equals("args")) {
List<String> arguments =
(List<String>) (capsOptions.getOrDefault(("args"), new ArrayList<>()));

if (!toReturn.containsKey(options)) {
toReturn.put(options, stereotypeOptions);
return new ImmutableCapabilities(toReturn);
List<String> stereotypeArguments =
(List<String>) (stereotypeOptions.getOrDefault(("args"), new ArrayList<>()));

arguments.forEach(arg -> {
if (!stereotypeArguments.contains(arg)) {
stereotypeArguments.add(arg);
}
});
toReturn.put("args", stereotypeArguments);
}

if (name.equals("extensions")) {
List<String> extensionList = (List<String>) (capsOptions.get(("extensions")));

List<String> stereotypeExtensions =
(List<String>) (stereotypeOptions.getOrDefault(("extensions"), new ArrayList<>()));

extensionList.forEach(extension -> {
if (!stereotypeExtensions.contains(extension)) {
stereotypeExtensions.add(extension);
}
});

toReturn.put("extensions", stereotypeExtensions);
}

if (name.equals("binary")) {
Object binary = capsOptions.get("binary");
toReturn.put("binary", binary);
}
pujagani marked this conversation as resolved.
Show resolved Hide resolved

if (!name.equals("binary") && !name.equals("extensions") && !name.equals("args")) {
toReturn.put(name, capsOptions.get(name));
}
}

@SuppressWarnings("unchecked")
Map<String, Object> capsOptions = (Map<String, Object>) toReturn.get(options);
stereotypeOptions.forEach((key, value) -> {
if (!capsOptions.containsKey(key)) {
capsOptions.put(key, value);
return toReturn;
}

private Map<String, Object> mergeFirefoxOptions(Map<String, Object> stereotypeOptions,
Map<String, Object> capsOptions) {
Map<String, Object> toReturn = new HashMap<>(stereotypeOptions);

for (String name : capsOptions.keySet()) {
if (name.equals("args")) {
List<String>
arguments =
(List<String>) (capsOptions.getOrDefault(("args"), new ArrayList<>()));
List<String> stereotypeArguments =
(List<String>) (stereotypeOptions.getOrDefault(("args"), new ArrayList<>()));
arguments.forEach(arg -> {
if (!stereotypeArguments.contains(arg)) {
stereotypeArguments.add(arg);
}
});
toReturn.put("args", stereotypeArguments);
}

if (name.equals("prefs")) {
Map<String, Object> prefs =
(Map<String, Object>) (capsOptions.getOrDefault(("prefs"), new HashMap<>()));

Map<String, Object> stereotypePrefs =
(Map<String, Object>) (stereotypeOptions.getOrDefault(("prefs"), new HashMap<>()));

stereotypePrefs.putAll(prefs);
toReturn.put("prefs", stereotypePrefs);
}
});

return new ImmutableCapabilities(toReturn);
if (name.equals("profile")) {
String rawProfile =
(String) capsOptions.get("profile");

toReturn.put("profile", rawProfile);
}

if (name.equals("binary")) {
Object binary = capsOptions.get("binary");
toReturn.put("binary", binary);
}
pujagani marked this conversation as resolved.
Show resolved Hide resolved

if (name.equals("log")) {
Map<String, Object> logLevelMap =
(Map<String, Object>) capsOptions.get("log");
toReturn.put("log", logLevelMap);
}
}
pujagani marked this conversation as resolved.
Show resolved Hide resolved

return toReturn;
}
}
Loading