@@ -82,7 +82,7 @@ public class FirefoxOptions {
8282 private Map <String , Integer > intPrefs = new HashMap <>();
8383 private Map <String , String > stringPrefs = new HashMap <>();
8484 private Level logLevel = null ;
85- private Boolean legacy ;
85+ private boolean legacy ;
8686 private DesiredCapabilities desiredCapabilities = new DesiredCapabilities ();
8787 private DesiredCapabilities requiredCapabilities = new DesiredCapabilities ();
8888
@@ -143,6 +143,31 @@ private static <T> T getOption(Map<String, Object> map, String key, Class<T> typ
143143 "In FirefoxOptions, expected key '%s' to be a %s: %s" , key , type .getSimpleName (), map ));
144144 }
145145
146+ public FirefoxOptions () {
147+ // Read system properties and use those if they are set, allowing users to override them later
148+ // should they want to.
149+
150+ String binary = System .getProperty (FirefoxDriver .SystemProperty .BROWSER_BINARY );
151+ if (binary != null ) {
152+ setBinary (binary );
153+ }
154+
155+ String forceMarionette = System .getProperty (FirefoxDriver .SystemProperty .DRIVER_USE_MARIONETTE );
156+ if (forceMarionette != null ) {
157+ setLegacy (!Boolean .getBoolean (FirefoxDriver .SystemProperty .DRIVER_USE_MARIONETTE ));
158+ }
159+
160+ String profileName = System .getProperty (FirefoxDriver .SystemProperty .BROWSER_PROFILE );
161+ if (profileName != null ) {
162+ this .profile = new ProfilesIni ().getProfile (profileName );
163+ if (this .profile == null ) {
164+ throw new WebDriverException (String .format (
165+ "Firefox profile '%s' named in system property '%s' not found" ,
166+ profileName , FirefoxDriver .SystemProperty .BROWSER_PROFILE ));
167+ }
168+ }
169+ }
170+
146171 public FirefoxOptions setLegacy (boolean legacy ) {
147172 this .legacy = legacy ;
148173 desiredCapabilities .setCapability (MARIONETTE , !legacy );
@@ -151,15 +176,7 @@ public FirefoxOptions setLegacy(boolean legacy) {
151176 }
152177
153178 public boolean isLegacy () {
154- String forceMarionette = System .getProperty (FirefoxDriver .SystemProperty .DRIVER_USE_MARIONETTE );
155- if (forceMarionette != null ) {
156- return !Boolean .valueOf (forceMarionette );
157- }
158- if (legacy != null ) {
159- return legacy ;
160- }
161-
162- return false ;
179+ return legacy ;
163180 }
164181
165182 public FirefoxOptions setBinary (FirefoxBinary binary ) {
@@ -256,6 +273,10 @@ public FirefoxOptions setProfile(FirefoxProfile profile) {
256273 }
257274
258275 public FirefoxProfile getProfile () {
276+ return getProfileOrNull ().orElseGet (() -> fullyPopulateProfile (new FirefoxProfile ()));
277+ }
278+
279+ public Optional <FirefoxProfile > getProfileOrNull () {
259280 FirefoxProfile profileToUse = profile ;
260281 if (profileToUse == null ) {
261282 profileToUse = extractProfile (requiredCapabilities );
@@ -264,29 +285,21 @@ public FirefoxProfile getProfile() {
264285 profileToUse = extractProfile (desiredCapabilities );
265286 }
266287 if (profileToUse == null ) {
267- String suggestedProfile = System .getProperty (FirefoxDriver .SystemProperty .BROWSER_PROFILE );
268- if (suggestedProfile != null ) {
269- profileToUse = new ProfilesIni ().getProfile (suggestedProfile );
270- if (profileToUse == null ) {
271- throw new WebDriverException (String .format (
272- "Firefox profile '%s' named in system property '%s' not found" ,
273- suggestedProfile , FirefoxDriver .SystemProperty .BROWSER_PROFILE ));
274- }
275- }
276- }
277- if (profileToUse == null ) {
278- profileToUse = new FirefoxProfile ();
288+ return Optional .empty ();
279289 }
280290
281- populateProfile (profileToUse , desiredCapabilities );
282- populateProfile (profileToUse , requiredCapabilities );
291+ return Optional .of (fullyPopulateProfile (profileToUse ));
292+ }
293+
294+ private FirefoxProfile fullyPopulateProfile (FirefoxProfile profile ) {
295+ populateProfile (profile , desiredCapabilities );
296+ populateProfile (profile , requiredCapabilities );
283297
284- FirefoxProfile prefHolder = profileToUse ;
285- booleanPrefs .entrySet ().forEach (pref -> prefHolder .setPreference (pref .getKey (), pref .getValue ()));
286- intPrefs .entrySet ().forEach (pref -> prefHolder .setPreference (pref .getKey (), pref .getValue ()));
287- stringPrefs .entrySet ().forEach (pref -> prefHolder .setPreference (pref .getKey (), pref .getValue ()));
298+ booleanPrefs .entrySet ().forEach (pref -> profile .setPreference (pref .getKey (), pref .getValue ()));
299+ intPrefs .entrySet ().forEach (pref -> profile .setPreference (pref .getKey (), pref .getValue ()));
300+ stringPrefs .entrySet ().forEach (pref -> profile .setPreference (pref .getKey (), pref .getValue ()));
288301
289- return profileToUse ;
302+ return profile ;
290303 }
291304
292305 private static void populateProfile (FirefoxProfile profile , Capabilities capabilities ) {
0 commit comments