Skip to content

Commit

Permalink
[java] Merge capabilities of slot with the new session request capabi…
Browse files Browse the repository at this point in the history
…lities (#11369)
  • Loading branch information
pujagani authored Jan 30, 2023
1 parent 8815c27 commit 6bc5a58
Show file tree
Hide file tree
Showing 2 changed files with 408 additions and 20 deletions.
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,128 @@ 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.toLowerCase()) {
case "chrome":
case "microsoftedge":
case "msedge":
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);
}

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

if (!toReturn.containsKey(options)) {
toReturn.put(options, stereotypeOptions);
return new ImmutableCapabilities(toReturn);
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("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") && !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);
}

if (name.equals("profile")) {
String rawProfile =
(String) capsOptions.get("profile");

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

if (name.equals("log")) {
Map<String, Object> logLevelMap =
(Map<String, Object>) capsOptions.get("log");
toReturn.put("log", logLevelMap);
}
});
}

return new ImmutableCapabilities(toReturn);
return toReturn;
}
}
Loading

0 comments on commit 6bc5a58

Please sign in to comment.