|
23 | 23 |
|
24 | 24 | import com.google.common.base.Preconditions; |
25 | 25 | import com.google.common.collect.ImmutableList; |
26 | | -import com.google.common.collect.ImmutableMap; |
27 | 26 | import com.google.gson.JsonArray; |
28 | 27 | import com.google.gson.JsonElement; |
29 | 28 | import com.google.gson.JsonObject; |
30 | 29 | import com.google.gson.JsonPrimitive; |
31 | 30 |
|
32 | | -import org.openqa.selenium.logging.LogLevelMapping; |
| 31 | +import org.openqa.selenium.WebDriverException; |
33 | 32 | import org.openqa.selenium.remote.DesiredCapabilities; |
34 | 33 |
|
35 | 34 | import java.io.File; |
@@ -69,6 +68,63 @@ public class FirefoxOptions { |
69 | 68 | private Map<String, String> stringPrefs = new HashMap<>(); |
70 | 69 | private Level logLevel = null; |
71 | 70 |
|
| 71 | + /** INTERNAL ONLY: DO NOT USE */ |
| 72 | + static FirefoxOptions fromJsonMap(Map<String, Object> map) throws IOException { |
| 73 | + FirefoxOptions options = new FirefoxOptions(); |
| 74 | + |
| 75 | + if (map.containsKey("binary")) { |
| 76 | + options.setBinary(getOption(map, "binary", String.class)); |
| 77 | + } |
| 78 | + |
| 79 | + if (map.containsKey("args")) { |
| 80 | + @SuppressWarnings("unchecked") // #YOLO |
| 81 | + List<String> list = (List) getOption(map, "args", List.class); |
| 82 | + options.addArguments(list); |
| 83 | + } |
| 84 | + |
| 85 | + if (map.containsKey("profile")) { |
| 86 | + Object value = map.get("profile"); |
| 87 | + if (value instanceof String) { |
| 88 | + options.setProfile(FirefoxProfile.fromJson((String) value)); |
| 89 | + } else if (value instanceof FirefoxProfile) { |
| 90 | + options.setProfile((FirefoxProfile) value); |
| 91 | + } else { |
| 92 | + throw new WebDriverException( |
| 93 | + "In FirefoxOptions, don't know how to convert profile: " + map); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (map.containsKey("prefs")) { |
| 98 | + @SuppressWarnings("unchecked") // #YOLO |
| 99 | + Map<String, Object> prefs = (Map) getOption(map, "prefs", Map.class); |
| 100 | + prefs.entrySet().forEach(entry -> { |
| 101 | + Object value = entry.getValue(); |
| 102 | + if (value instanceof Boolean) { |
| 103 | + options.addPreference(entry.getKey(), (Boolean) value); |
| 104 | + } else if (value instanceof Integer) { |
| 105 | + options.addPreference(entry.getKey(), (Integer) value); |
| 106 | + } else if (value instanceof String) { |
| 107 | + options.addPreference(entry.getKey(), (String) value); |
| 108 | + } else { |
| 109 | + throw new WebDriverException( |
| 110 | + "Invalid Firefox preference value: " + entry.getKey() + "=" + value); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return options; |
| 116 | + } |
| 117 | + |
| 118 | + private static <T> T getOption(Map<String, Object> map, String key, Class<T> type) { |
| 119 | + Object value = map.get(key); |
| 120 | + if (type.isInstance(value)) { |
| 121 | + return type.cast(value); |
| 122 | + } |
| 123 | + throw new WebDriverException( |
| 124 | + String.format( |
| 125 | + "In FirefoxOptions, expected key '%s' to be a %s: %s", key, type.getSimpleName(), map)); |
| 126 | + } |
| 127 | + |
72 | 128 | public FirefoxOptions setBinary(Path path) { |
73 | 129 | return setBinary(checkNotNull(path).toString()); |
74 | 130 | } |
|
0 commit comments