Skip to content

Commit 4acae8c

Browse files
committed
Refactoring constructor that accepts required capabilities too. But it should be reworked later to fit W3C standard
1 parent 59f8fef commit 4acae8c

File tree

2 files changed

+56
-45
lines changed

2 files changed

+56
-45
lines changed

java/client/src/org/openqa/selenium/firefox/FirefoxDriver.java

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.openqa.selenium.remote.CapabilityType.VERSION;
2626

2727
import com.google.common.base.Preconditions;
28-
import com.google.common.base.Predicate;
2928
import com.google.common.collect.ImmutableMap;
3029
import com.google.common.collect.Maps;
3130
import com.google.common.collect.Sets;
@@ -62,8 +61,7 @@
6261
import java.util.concurrent.TimeUnit;
6362

6463
/**
65-
* An implementation of the {#link WebDriver} interface that drives Firefox. This works through a
66-
* firefox extension, which gets installed automatically if necessary.
64+
* An implementation of the {#link WebDriver} interface that drives Firefox.
6765
*/
6866
public class FirefoxDriver extends RemoteWebDriver implements Killable {
6967

@@ -120,7 +118,7 @@ public static final class SystemProperty {
120118

121119
// TODO: make it public as soon as it's fully implemented
122120
FirefoxDriver(FirefoxOptions options) {
123-
super(toExecutor(options), options.toDesiredCapabilities(), null);
121+
this(toExecutor(options), options.toDesiredCapabilities(), options.toRequiredCapabilities());
124122
//binary = options.getBinary();
125123
}
126124

@@ -182,14 +180,21 @@ private static FirefoxOptions getFirefoxOptions(Capabilities capabilities) {
182180
}
183181

184182
public FirefoxDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
185-
this(getBinary(desiredCapabilities), null, desiredCapabilities, requiredCapabilities);
183+
this(getFirefoxOptions(desiredCapabilities).addDesiredCapabilities(desiredCapabilities)
184+
.addRequiredCapabilities(requiredCapabilities));
186185
}
187186

188187
public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities capabilities) {
189188
this(getFirefoxOptions(capabilities).setBinary(binary).setProfile(profile)
190189
.addDesiredCapabilities(capabilities));
191190
}
192191

192+
public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile,
193+
Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
194+
this(getFirefoxOptions(desiredCapabilities).setBinary(binary).setProfile(profile)
195+
.addDesiredCapabilities(desiredCapabilities).addRequiredCapabilities(requiredCapabilities));
196+
}
197+
193198
private static FirefoxProfile prepareProfile(FirefoxProfile profile,
194199
Capabilities desiredCapabilities,
195200
Capabilities requiredCapabilities) {
@@ -297,14 +302,6 @@ private static FirefoxBinary getBinary(Capabilities capabilities) {
297302
return new FirefoxBinary();
298303
}
299304

300-
public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile,
301-
Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
302-
this(createCommandExecutor(desiredCapabilities, requiredCapabilities, binary, profile),
303-
addProfileTo(desiredCapabilities, prepareProfile(profile, desiredCapabilities, requiredCapabilities)),
304-
requiredCapabilities);
305-
this.binary = binary;
306-
}
307-
308305
public FirefoxDriver(GeckoDriverService driverService) {
309306
this(new DriverCommandExecutor(driverService), null, null);
310307
}
@@ -325,23 +322,6 @@ private FirefoxDriver(CommandExecutor executor, Capabilities desiredCapabilities
325322
dropCapabilities(requiredCapabilities));
326323
}
327324

328-
private static CommandExecutor createCommandExecutor(Capabilities desiredCapabilities,
329-
Capabilities requiredCapabilities,
330-
FirefoxBinary binary,
331-
FirefoxProfile profile) {
332-
if (isLegacy(desiredCapabilities)) {
333-
if (profile == null) {
334-
profile = extractProfile(desiredCapabilities, requiredCapabilities);
335-
}
336-
return new LazyCommandExecutor(binary, profile);
337-
}
338-
GeckoDriverService.Builder builder = new GeckoDriverService.Builder().usingPort(0);
339-
if (binary != null) {
340-
builder.usingFirefoxBinary(binary);
341-
}
342-
return new DriverCommandExecutor(builder.build());
343-
}
344-
345325
@Override
346326
public void setFileDetector(FileDetector detector) {
347327
throw new WebDriverException(
@@ -470,11 +450,8 @@ private static Capabilities dropCapabilities(Capabilities capabilities) {
470450

471451
if (isLegacy(capabilities)) {
472452
final Set<String> toRemove = Sets.newHashSet(BINARY, PROFILE);
473-
caps = new DesiredCapabilities(Maps.filterKeys(capabilities.asMap(), new Predicate<String>() {
474-
public boolean apply(String key) {
475-
return !toRemove.contains(key);
476-
}
477-
}));
453+
caps = new DesiredCapabilities(
454+
Maps.filterKeys(capabilities.asMap(), key -> !toRemove.contains(key)));
478455
} else {
479456
caps = new DesiredCapabilities(capabilities);
480457
}

java/client/src/org/openqa/selenium/firefox/FirefoxOptions.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import org.openqa.selenium.Capabilities;
3131
import org.openqa.selenium.WebDriverException;
32-
import org.openqa.selenium.firefox.internal.ProfilesIni;
3332
import org.openqa.selenium.remote.DesiredCapabilities;
3433

3534
import java.io.IOException;
@@ -68,6 +67,7 @@ public class FirefoxOptions {
6867
private Level logLevel = null;
6968
private Boolean legacy;
7069
private DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
70+
private DesiredCapabilities requiredCapabilities = new DesiredCapabilities();
7171

7272
/** INTERNAL ONLY: DO NOT USE */
7373
static FirefoxOptions fromJsonMap(Map<String, Object> map) throws IOException {
@@ -172,12 +172,21 @@ public FirefoxOptions setProfile(FirefoxProfile profile) {
172172
}
173173

174174
public FirefoxProfile getProfile() {
175-
final FirefoxProfile toReturn = Optional.ofNullable(profile).orElse(
176-
Optional.ofNullable(extractProfile()).orElse(new FirefoxProfile()));
175+
FirefoxProfile toReturn = profile;
176+
if (toReturn == null) {
177+
toReturn = extractProfile(desiredCapabilities);
178+
}
179+
if (toReturn == null) {
180+
toReturn = extractProfile(requiredCapabilities);
181+
}
182+
if (toReturn == null) {
183+
toReturn = new FirefoxProfile();
184+
}
177185

178-
booleanPrefs.entrySet().forEach(pref -> toReturn.setPreference(pref.getKey(), pref.getValue()));
179-
intPrefs.entrySet().forEach(pref -> toReturn.setPreference(pref.getKey(), pref.getValue()));
180-
stringPrefs.entrySet().forEach(pref -> toReturn.setPreference(pref.getKey(), pref.getValue()));
186+
FirefoxProfile prefHolder = toReturn;
187+
booleanPrefs.entrySet().forEach(pref -> prefHolder.setPreference(pref.getKey(), pref.getValue()));
188+
intPrefs.entrySet().forEach(pref -> prefHolder.setPreference(pref.getKey(), pref.getValue()));
189+
stringPrefs.entrySet().forEach(pref -> prefHolder.setPreference(pref.getKey(), pref.getValue()));
181190

182191
return toReturn;
183192
}
@@ -223,7 +232,7 @@ public FirefoxOptions setLogLevel(Level logLevel) {
223232
public FirefoxOptions addDesiredCapabilities(Capabilities desiredCapabilities) {
224233
this.desiredCapabilities.merge(desiredCapabilities);
225234

226-
FirefoxProfile suggestedProfile = extractProfile();
235+
FirefoxProfile suggestedProfile = extractProfile(desiredCapabilities);
227236
if (suggestedProfile != null) {
228237
if (!booleanPrefs.isEmpty() || !intPrefs.isEmpty() || !stringPrefs.isEmpty()) {
229238
throw new IllegalStateException(
@@ -241,11 +250,32 @@ public FirefoxOptions addDesiredCapabilities(Capabilities desiredCapabilities) {
241250
return this;
242251
}
243252

244-
private FirefoxProfile extractProfile() {
245-
if (desiredCapabilities == null) {
253+
public FirefoxOptions addRequiredCapabilities(Capabilities requiredCapabilities) {
254+
this.requiredCapabilities.merge(requiredCapabilities);
255+
256+
FirefoxProfile suggestedProfile = extractProfile(desiredCapabilities);
257+
if (suggestedProfile != null) {
258+
if (!booleanPrefs.isEmpty() || !intPrefs.isEmpty() || !stringPrefs.isEmpty()) {
259+
throw new IllegalStateException(
260+
"Unable to determine if preferences set on this option " +
261+
"are the same as the profile in the capabilities");
262+
}
263+
if (profile != null && !suggestedProfile.equals(profile)) {
264+
throw new IllegalStateException(
265+
"Profile has been set on both the capabilities and these options, but they're " +
266+
"different. Unable to determine which one you want to use.");
267+
}
268+
profile = suggestedProfile;
269+
}
270+
271+
return this;
272+
}
273+
274+
private FirefoxProfile extractProfile(Capabilities capabilities) {
275+
if (capabilities == null) {
246276
return null;
247277
}
248-
Object raw = desiredCapabilities.getCapability(PROFILE);
278+
Object raw = capabilities.getCapability(PROFILE);
249279
if (raw == null) {
250280
return null;
251281

@@ -304,6 +334,10 @@ public Capabilities toDesiredCapabilities() {
304334
return capabilities;
305335
}
306336

337+
public Capabilities toRequiredCapabilities() {
338+
return requiredCapabilities;
339+
}
340+
307341
public DesiredCapabilities addTo(DesiredCapabilities capabilities) {
308342
capabilities.merge(toDesiredCapabilities());
309343
return capabilities;

0 commit comments

Comments
 (0)