Skip to content

Commit faa5179

Browse files
committed
java: Adding getters to FirefoxProfile to allow reading preset preference values
1 parent 868512b commit faa5179

File tree

2 files changed

+67
-31
lines changed

2 files changed

+67
-31
lines changed

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

+30-14
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public FirefoxProfile() {
7070
* Constructs a firefox profile from an existing profile directory.
7171
* <p>
7272
* Users who need this functionality should consider using a named profile.
73-
*
73+
*
7474
* @param profileDir The profile directory to use as a model.
7575
*/
7676
public FirefoxProfile(File profileDir) {
@@ -143,10 +143,26 @@ private boolean getBooleanPreference(Preferences prefs, String key, boolean defa
143143
throw new WebDriverException("Expected boolean value is not a boolean. It is: " + value);
144144
}
145145

146+
public String getStringPreference(String key, String defaultValue) {
147+
Object preference = additionalPrefs.getPreference(key);
148+
if(preference != null && preference instanceof String){
149+
return (String) preference;
150+
}
151+
return defaultValue;
152+
}
153+
146154
public int getIntegerPreference(String key, int defaultValue) {
147-
Object preference=additionalPrefs.getPreference(key);
148-
if(preference!=null && preference instanceof Integer){
149-
return (Integer)preference;
155+
Object preference = additionalPrefs.getPreference(key);
156+
if(preference != null && preference instanceof Integer){
157+
return (Integer) preference;
158+
}
159+
return defaultValue;
160+
}
161+
162+
public boolean getBooleanPreference(String key, boolean defaultValue) {
163+
Object preference = additionalPrefs.getPreference(key);
164+
if(preference != null && preference instanceof Boolean){
165+
return (Boolean) preference;
150166
}
151167
return defaultValue;
152168
}
@@ -184,7 +200,7 @@ public void addExtension(Class<?> loadResourcesUsing, String loadFrom) throws IO
184200

185201
/**
186202
* Attempt to add an extension to install into this instance.
187-
*
203+
*
188204
* @param extensionToInstall
189205
* @throws IOException
190206
*/
@@ -210,7 +226,7 @@ private String deriveExtensionName(String originalName) {
210226
* Note that if a value looks as if it is a quoted string (that is, starts with a quote character
211227
* and ends with one too) an IllegalArgumentException is thrown: Firefox fails to start properly
212228
* when some values are set to this.
213-
*
229+
*
214230
* @param key The key
215231
* @param value The new value.
216232
*/
@@ -220,7 +236,7 @@ public void setPreference(String key, String value) {
220236

221237
/**
222238
* Set a preference for this particular profile.
223-
*
239+
*
224240
* @param key The key
225241
* @param value The new value.
226242
*/
@@ -230,7 +246,7 @@ public void setPreference(String key, boolean value) {
230246

231247
/**
232248
* Set a preference for this particular profile.
233-
*
249+
*
234250
* @param key The key
235251
* @param value The new value.
236252
*/
@@ -315,7 +331,7 @@ public void setEnableNativeEvents(boolean enableNativeEvents) {
315331
/**
316332
* Returns whether the no focus library should be loaded for Firefox profiles launched on Linux,
317333
* even if native events are disabled.
318-
*
334+
*
319335
* @return Whether the no focus library should always be loaded for Firefox on Linux.
320336
*/
321337
public boolean shouldLoadNoFocusLib() {
@@ -324,7 +340,7 @@ public boolean shouldLoadNoFocusLib() {
324340

325341
/**
326342
* Sets whether the no focus library should always be loaded on Linux.
327-
*
343+
*
328344
* @param loadNoFocusLib Whether to always load the no focus library.
329345
*/
330346
public void setAlwaysLoadNoFocusLib(boolean loadNoFocusLib) {
@@ -334,7 +350,7 @@ public void setAlwaysLoadNoFocusLib(boolean loadNoFocusLib) {
334350
/**
335351
* Sets whether Firefox should accept SSL certificates which have expired, signed by an unknown
336352
* authority or are generally untrusted. This is set to true by default.
337-
*
353+
*
338354
* @param acceptUntrustedSsl Whether untrusted SSL certificates should be accepted.
339355
*/
340356

@@ -353,7 +369,7 @@ public void setAcceptUntrustedCertificates(boolean acceptUntrustedSsl) {
353369
* If you are receive an "untrusted site" prompt on Firefox when using a certificate that was
354370
* issued by valid issuer, but has expired or is being served served for a different host (e.g.
355371
* production certificate served in a testing environment) set this to false.
356-
*
372+
*
357373
* @param untrustedIssuer whether to assume untrusted issuer or not.
358374
*/
359375
public void setAssumeUntrustedCertificateIssuer(boolean untrustedIssuer) {
@@ -386,10 +402,10 @@ protected void cleanTemporaryModel() {
386402
* Call this to cause the current profile to be written to disk. The profile directory is
387403
* returned. Note that this profile directory is a temporary one and will be deleted when the JVM
388404
* exists (at the latest)
389-
*
405+
*
390406
* This method should be called immediately before starting to use the profile and should only be
391407
* called once per instance of the {@link org.openqa.selenium.firefox.FirefoxDriver}.
392-
*
408+
*
393409
* @return The directory containing the profile.
394410
*/
395411
public File layoutOnDisk() {

java/client/test/org/openqa/selenium/firefox/FirefoxProfileTest.java

+37-17
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,32 @@ public void setUp() throws Exception {
5757
public void shouldQuoteStringsWhenSettingStringProperties() throws Exception {
5858
profile.setPreference("cheese", "brie");
5959

60-
List<String> props = readGeneratedProperties(profile);
61-
boolean seenCheese = false;
62-
for (String line : props) {
63-
if (line.contains("cheese") && line.contains("\"brie\"")) {
64-
seenCheese = true;
65-
}
66-
}
60+
assertPreferenceValueEquals("cheese", "\"brie\"");
61+
}
6762

68-
assertTrue(seenCheese);
63+
@Test
64+
public void getStringPreferenceShouldReturnUserSuppliedValueWhenSet() throws Exception {
65+
String key = "cheese";
66+
String value = "brie";
67+
profile.setPreference(key, value);
68+
69+
String defaultValue = "edam";
70+
assertEquals(value, profile.getStringPreference(key, defaultValue));
71+
}
72+
73+
@Test
74+
public void getStringPreferenceShouldReturnDefaultValueWhenSet() throws Exception {
75+
String key = "cheese";
76+
77+
String defaultValue = "brie";
78+
assertEquals(defaultValue, profile.getStringPreference(key, defaultValue));
6979
}
7080

7181
@Test
7282
public void shouldSetIntegerPreferences() throws Exception {
7383
profile.setPreference("cheese", 1234);
7484

75-
List<String> props = readGeneratedProperties(profile);
76-
boolean seenCheese = false;
77-
for (String line : props) {
78-
if (line.contains("cheese") && line.contains(", 1234)")) {
79-
seenCheese = true;
80-
}
81-
}
82-
83-
assertTrue("Did not see integer value being set correctly", seenCheese);
85+
assertPreferenceValueEquals("cheese", 1234);
8486
}
8587

8688
@Test
@@ -108,6 +110,24 @@ public void shouldSetBooleanPreferences() throws Exception {
108110
assertPreferenceValueEquals("cheese", false);
109111
}
110112

113+
@Test
114+
public void getBooleanPreferenceShouldReturnUserSuppliedValueWhenSet() throws Exception {
115+
String key = "cheese";
116+
boolean value = true;
117+
profile.setPreference(key, value);
118+
119+
boolean defaultValue = false;
120+
assertEquals(value, profile.getBooleanPreference(key, defaultValue));
121+
}
122+
123+
@Test
124+
public void getBooleanPreferenceShouldReturnDefaultValueWhenSet() throws Exception {
125+
String key = "cheese";
126+
127+
boolean defaultValue = true;
128+
assertEquals(defaultValue, profile.getBooleanPreference(key, defaultValue));
129+
}
130+
111131
@Test
112132
public void shouldSetDefaultPreferences() throws Exception {
113133
assertPreferenceValueEquals("network.http.phishy-userpass-length", 255);

0 commit comments

Comments
 (0)