|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.firefox; |
| 19 | + |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | +import static org.openqa.selenium.firefox.FirefoxDriver.BINARY; |
| 22 | +import static org.openqa.selenium.firefox.FirefoxDriver.PROFILE; |
| 23 | + |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.gson.JsonArray; |
| 26 | +import com.google.gson.JsonElement; |
| 27 | +import com.google.gson.JsonObject; |
| 28 | +import com.google.gson.JsonPrimitive; |
| 29 | + |
| 30 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +public class FirefoxOptions { |
| 41 | + |
| 42 | + private String binary; |
| 43 | + private FirefoxProfile profile; |
| 44 | + private List<String> args = new ArrayList<>(); |
| 45 | + private Map<String, Boolean> booleanPrefs = new HashMap<>(); |
| 46 | + private Map<String, Integer> intPrefs = new HashMap<>(); |
| 47 | + private Map<String, String> stringPrefs = new HashMap<>(); |
| 48 | + |
| 49 | + public FirefoxOptions setBinary(Path path) { |
| 50 | + return setBinary(checkNotNull(path).toString()); |
| 51 | + } |
| 52 | + |
| 53 | + public FirefoxOptions setBinary(String binary) { |
| 54 | + this.binary = checkNotNull(binary); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public FirefoxOptions setProfile(FirefoxProfile profile) { |
| 59 | + this.profile = checkNotNull(profile); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public FirefoxOptions addArguments(String... arguments) { |
| 64 | + addArguments(ImmutableList.copyOf(arguments)); |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public FirefoxOptions addArguments(List<String> arguments) { |
| 69 | + args.addAll(arguments); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public FirefoxOptions addPreference(String key, boolean value) { |
| 74 | + booleanPrefs.put(checkNotNull(key), value); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public FirefoxOptions addPreference(String key, int value) { |
| 79 | + intPrefs.put(checkNotNull(key), value); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public FirefoxOptions addPreference(String key, String value) { |
| 84 | + stringPrefs.put(checkNotNull(key), checkNotNull(value)); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public DesiredCapabilities addTo(DesiredCapabilities capabilities) { |
| 89 | + Object priorBinary = capabilities.getCapability(BINARY); |
| 90 | + if (binary != null && priorBinary != null && !binary.equals(priorBinary)) { |
| 91 | + throw new IllegalStateException( |
| 92 | + "Binary already set in capabilities, but is different from the one in these options"); |
| 93 | + } |
| 94 | + |
| 95 | + Object priorProfile = capabilities.getCapability(PROFILE); |
| 96 | + if (priorProfile != null) { |
| 97 | + if (!booleanPrefs.isEmpty() || !intPrefs.isEmpty() || !stringPrefs.isEmpty()) { |
| 98 | + throw new IllegalStateException( |
| 99 | + "Unable to determine if preferences set on this option " + |
| 100 | + "are the same as the profile in the capabilities"); |
| 101 | + } |
| 102 | + if (!priorProfile.equals(profile)) { |
| 103 | + throw new IllegalStateException( |
| 104 | + "Profile has been set on both the capabilities and these options, but they're " + |
| 105 | + "different. Unable to determine which one you want to use."); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + capabilities.setCapability("firefoxOptions", this); |
| 110 | + |
| 111 | + if (binary != null) { |
| 112 | + FirefoxBinary actualBinary = new FirefoxBinary(new File(binary)); |
| 113 | + actualBinary.addCommandLineOptions(args.toArray(new String[args.size()])); |
| 114 | + capabilities.setCapability(BINARY, actualBinary); |
| 115 | + } |
| 116 | + |
| 117 | + if (profile != null) { |
| 118 | + capabilities.setCapability(PROFILE, profile); |
| 119 | + } |
| 120 | + |
| 121 | + return capabilities; |
| 122 | + } |
| 123 | + |
| 124 | + public JsonElement toJson() throws IOException { |
| 125 | + JsonObject options = new JsonObject(); |
| 126 | + |
| 127 | + if (binary != null) { |
| 128 | + options.addProperty("binary", binary); |
| 129 | + } |
| 130 | + |
| 131 | + if (profile != null) { |
| 132 | + for (Map.Entry<String, Boolean> pref : booleanPrefs.entrySet()) { |
| 133 | + profile.setPreference(pref.getKey(), pref.getValue()); |
| 134 | + } |
| 135 | + for (Map.Entry<String, Integer> pref : intPrefs.entrySet()) { |
| 136 | + profile.setPreference(pref.getKey(), pref.getValue()); |
| 137 | + } |
| 138 | + for (Map.Entry<String, String> pref : stringPrefs.entrySet()) { |
| 139 | + profile.setPreference(pref.getKey(), pref.getValue()); |
| 140 | + } |
| 141 | + options.addProperty("profile", profile.toJson()); |
| 142 | + } else { |
| 143 | + JsonObject allPrefs = new JsonObject(); |
| 144 | + for (Map.Entry<String, Boolean> pref : booleanPrefs.entrySet()) { |
| 145 | + allPrefs.add(pref.getKey(), new JsonPrimitive(pref.getValue())); |
| 146 | + } |
| 147 | + for (Map.Entry<String, Integer> pref : intPrefs.entrySet()) { |
| 148 | + allPrefs.add(pref.getKey(), new JsonPrimitive(pref.getValue())); |
| 149 | + } |
| 150 | + for (Map.Entry<String, String> pref : stringPrefs.entrySet()) { |
| 151 | + allPrefs.add(pref.getKey(), new JsonPrimitive(pref.getValue())); |
| 152 | + } |
| 153 | + options.add("prefs", allPrefs); |
| 154 | + } |
| 155 | + |
| 156 | + JsonArray arguments = new JsonArray(); |
| 157 | + for (String arg : args) { |
| 158 | + arguments.add(new JsonPrimitive(arg)); |
| 159 | + } |
| 160 | + options.add("args", arguments); |
| 161 | + |
| 162 | + return options; |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments