Skip to content

Commit 8204b46

Browse files
Stephen Kuenzlibarancev
Stephen Kuenzli
authored andcommitted
Propagate webdriver_firefox_port preference to FirefoxDriver, was being ignored. Fixes issue 5172
Signed-off-by: Alexei Barantsev <[email protected]>
1 parent c813596 commit 8204b46

File tree

8 files changed

+130
-4
lines changed

8 files changed

+130
-4
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private static FirefoxProfile getProfile(FirefoxProfile profile) {
266266

267267
protected ExtensionConnection connectTo(FirefoxBinary binary, FirefoxProfile profile,
268268
String host) {
269-
Lock lock = obtainLock();
269+
Lock lock = obtainLock(profile);
270270
try {
271271
FirefoxBinary bin = binary == null ? new FirefoxBinary() : binary;
272272

@@ -283,8 +283,10 @@ protected ExtensionConnection connectTo(FirefoxBinary binary, FirefoxProfile pro
283283
}
284284
}
285285

286-
protected Lock obtainLock() {
287-
return new SocketLock();
286+
protected static Lock obtainLock(FirefoxProfile profile) {
287+
int preferredPort =
288+
profile.getIntegerPreference(FirefoxProfile.PORT_PREFERENCE, SocketLock.DEFAULT_PORT);
289+
return new SocketLock(preferredPort);
288290
}
289291

290292
@Override

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

+8
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ 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 int getIntegerPreference(String key, int defaultValue) {
147+
Object preference=additionalPrefs.getPreference(key);
148+
if(preference!=null && preference instanceof Integer){
149+
return (Integer)preference;
150+
}
151+
return defaultValue;
152+
}
153+
146154
private void verifyModel(File model) {
147155
if (model == null) {
148156
return;

java/client/src/org/openqa/selenium/firefox/internal/NewProfileExtensionConnection.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void start() throws IOException {
8383

8484
lock.lock(connectTimeout);
8585
try {
86-
port = determineNextFreePort(DEFAULT_PORT);
86+
port = determineNextFreePort(profile.getIntegerPreference(PORT_PREFERENCE, DEFAULT_PORT));
8787
profile.setPreference(PORT_PREFERENCE, port);
8888

8989
profileDir = profile.layoutOnDisk();

java/client/src/org/openqa/selenium/internal/SocketLock.java

+8
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,12 @@ private boolean isLockFree(InetSocketAddress address) throws IOException {
129129
return false;
130130
}
131131
}
132+
133+
/**
134+
* Gets the port number that is being-locked.
135+
* @return
136+
*/
137+
public int getLockPort(){
138+
return this.address.getPort();
139+
}
132140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.openqa.selenium.firefox;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import org.junit.Test;
9+
import org.openqa.selenium.internal.Lock;
10+
import org.openqa.selenium.internal.SocketLock;
11+
12+
/**
13+
* FirefoxDriverUtilitiesTest is responsible for tests of FirefoxDriver
14+
* utilities that do not require a browser.
15+
*/
16+
public class FirefoxDriverUtilitiesTest {
17+
18+
@Test
19+
public void shouldObtainSocketLockForDefaultPortWhenNotSpecifiedInProfile(){
20+
Lock lock = FirefoxDriver.obtainLock(new FirefoxProfile());
21+
22+
assertTrue("expected lock to be a SocketLock", lock instanceof SocketLock);
23+
24+
assertEquals(SocketLock.DEFAULT_PORT, ((SocketLock) lock).getLockPort());
25+
}
26+
27+
@Test
28+
public void shouldObtainSocketLockForPortSpecifiedInProfile(){
29+
FirefoxProfile mockProfile = mock(FirefoxProfile.class);
30+
int preferredPort = 2400;
31+
when(mockProfile.getIntegerPreference(FirefoxProfile.PORT_PREFERENCE, SocketLock.DEFAULT_PORT)).thenReturn(
32+
preferredPort);
33+
34+
Lock lock = FirefoxDriver.obtainLock(mockProfile);
35+
36+
assertTrue("expected lock to be a SocketLock", lock instanceof SocketLock);
37+
38+
assertEquals(preferredPort, ((SocketLock) lock).getLockPort());
39+
}
40+
41+
}

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

+18
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ public void shouldSetIntegerPreferences() throws Exception {
7878
assertTrue("Did not see integer value being set correctly", seenCheese);
7979
}
8080

81+
@Test
82+
public void getIntegerPreferenceShouldReturnUserSuppliedValueWhenSet() throws Exception {
83+
String key = "cheese";
84+
int value = 1234;
85+
profile.setPreference(key, value);
86+
87+
int defaultValue = -42;
88+
assertEquals(1234, profile.getIntegerPreference(key, defaultValue));
89+
}
90+
91+
@Test
92+
public void getIntegerPreferenceShouldReturnDefaultValueWhenSet() throws Exception {
93+
String key = "cheese";
94+
95+
int defaultValue = 42;
96+
assertEquals(defaultValue, profile.getIntegerPreference(key, defaultValue));
97+
}
98+
8199
@Test
82100
public void shouldSetBooleanPreferences() throws Exception {
83101
profile.setPreference("cheese", false);

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

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.runner.RunWith;
2121
import org.junit.runners.Suite;
2222
import org.openqa.selenium.firefox.internal.ExecutableTest;
23+
import org.openqa.selenium.firefox.internal.NewProfileExtensionConnectionTest;
2324
import org.openqa.selenium.firefox.internal.SocketLockTest;
2425
import org.openqa.selenium.firefox.internal.StreamsTest;
2526

@@ -29,8 +30,10 @@
2930
ExecutableTest.class,
3031
FirefoxCapabilitiesTest.class,
3132
FirefoxDriverTest.class,
33+
FirefoxDriverUtilitiesTest.class,
3234
FirefoxProfileTest.class,
3335
NativeEventsTest.class,
36+
NewProfileExtensionConnectionTest.class,
3437
PreferencesTest.class,
3538
SocketLockTest.class,
3639
StreamsTest.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.openqa.selenium.firefox.internal;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
6+
import org.junit.Test;
7+
import org.openqa.selenium.WebDriverException;
8+
import org.openqa.selenium.firefox.FirefoxBinary;
9+
import org.openqa.selenium.firefox.FirefoxProfile;
10+
import org.openqa.selenium.internal.SocketLock;
11+
12+
public class NewProfileExtensionConnectionTest {
13+
14+
@Test
15+
public void canBeConstructed() throws Exception {
16+
new NewProfileExtensionConnection
17+
(makeLock(), new FirefoxBinary(), new FirefoxProfile(), "my-host");
18+
}
19+
20+
@Test
21+
public void shouldDefaultToPortSpecifiedInProfileWhenDeterminingNextFreePort() throws Exception {
22+
int expectedPort = 2400;
23+
24+
FirefoxProfile profile = new FirefoxProfile();
25+
profile.setPreference(FirefoxProfile.PORT_PREFERENCE, expectedPort);
26+
27+
NewProfileExtensionConnection connection = new NewProfileExtensionConnection
28+
(makeLock(), new FirefoxBinary(), profile, "my-host");
29+
30+
try {
31+
32+
connection.start();
33+
34+
fail("there was an unexpected server listening on " + expectedPort + "; expected connection to fail");
35+
} catch (WebDriverException e) {
36+
int PORT_PREFERENCE_NOT_PROPAGATED = -1;
37+
assertEquals(expectedPort,
38+
profile.getIntegerPreference(FirefoxProfile.PORT_PREFERENCE, PORT_PREFERENCE_NOT_PROPAGATED));
39+
}
40+
41+
}
42+
43+
private SocketLock makeLock() {
44+
return new SocketLock(4200);
45+
}
46+
}

0 commit comments

Comments
 (0)