Skip to content

Commit c27cfac

Browse files
committed
add connectToExisting and withWebSocketPort methods
1 parent 843cb89 commit c27cfac

File tree

2 files changed

+71
-19
lines changed

2 files changed

+71
-19
lines changed

java/src/org/openqa/selenium/firefox/GeckoDriverService.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public static class Builder
147147
private @Nullable FirefoxDriverLogLevel logLevel;
148148
private @Nullable Boolean logTruncate;
149149
private @Nullable File profileRoot;
150+
private @Nullable Integer marionettePort;
151+
private @Nullable Integer websocketPort;
150152

151153
@Override
152154
public int score(Capabilities capabilities) {
@@ -204,6 +206,31 @@ public GeckoDriverService.Builder withProfileRoot(@Nullable File root) {
204206
return this;
205207
}
206208

209+
/**
210+
* Configures geckodriver to connect to an existing Firefox instance via the specified
211+
* Marionette port.
212+
*
213+
* @param marionettePort The port where Marionette is listening on the existing Firefox
214+
* instance.
215+
* @return A self reference.
216+
*/
217+
public GeckoDriverService.Builder connectToExisting(int marionettePort) {
218+
this.marionettePort = marionettePort;
219+
return this;
220+
}
221+
222+
/**
223+
* Configures the WebSocket port for BiDi. A value of 0 will automatically allocate a free port.
224+
*
225+
* @param websocketPort The port to use for WebSocket communication, or 0 for automatic
226+
* allocation.
227+
* @return A self reference.
228+
*/
229+
public GeckoDriverService.Builder withWebSocketPort(@Nullable Integer websocketPort) {
230+
this.websocketPort = websocketPort;
231+
return this;
232+
}
233+
207234
@Override
208235
protected void loadSystemProperties() {
209236
parseLogOutput(GECKO_DRIVER_LOG_PROPERTY);
@@ -229,27 +256,28 @@ protected List<String> createArgs() {
229256
List<String> args = new ArrayList<>();
230257
args.add(String.format(Locale.ROOT, "--port=%d", getPort()));
231258

232-
// Check if we're connecting to an existing Firefox instance
233-
boolean connectExisting = false;
234-
for (String arg : args) {
235-
if (arg.contains("--connect-existing")) {
236-
connectExisting = true;
237-
break;
259+
// Check if marionette port is specified via connectToExisting method
260+
if (marionettePort != null) {
261+
args.add("--connect-existing");
262+
args.add("--marionette-port");
263+
args.add(String.valueOf(marionettePort));
264+
} else {
265+
// Configure websocket port for BiDi communication
266+
if (websocketPort != null) {
267+
args.add("--websocket-port");
268+
args.add(String.valueOf(websocketPort));
269+
270+
args.add("--allow-origins");
271+
args.add(String.format("http://127.0.0.1:%d", websocketPort));
272+
args.add(String.format("http://localhost:%d", websocketPort));
273+
args.add(String.format("http://[::1]:%d", websocketPort));
274+
} else {
275+
// Use 0 to auto-allocate a free port
276+
args.add("--websocket-port");
277+
args.add("0");
238278
}
239279
}
240280

241-
// Only allocate a free port for the websocket when not connecting to an existing instance
242-
// This avoids conflicts when multiple Firefox instances are started
243-
if (!connectExisting) {
244-
int wsPort = PortProber.findFreePort();
245-
args.add(String.format("--websocket-port=%d", wsPort));
246-
247-
args.add("--allow-origins");
248-
args.add(String.format("http://127.0.0.1:%d", wsPort));
249-
args.add(String.format("http://localhost:%d", wsPort));
250-
args.add(String.format("http://[::1]:%d", wsPort));
251-
}
252-
253281
if (logLevel != null) {
254282
args.add("--log");
255283
args.add(logLevel.toString());

java/test/org/openqa/selenium/firefox/FirefoxDriverConcurrentTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void shouldBeAbleToUseTheSameProfileMoreThanOnce() {
168168

169169
@Test
170170
void multipleFirefoxInstancesWithBiDiEnabledCanRunSimultaneously() {
171-
// Create two Firefox instances with BiDi enabled
171+
// Create two Firefox instances with BiDi enabled, should use different ports
172172
FirefoxOptions options1 = new FirefoxOptions().enableBiDi();
173173
FirefoxOptions options2 = new FirefoxOptions().enableBiDi();
174174

@@ -206,4 +206,28 @@ void multipleFirefoxInstancesWithBiDiEnabledCanRunSimultaneously() {
206206
}
207207
}
208208
}
209+
210+
@Test
211+
void geckoDriverServiceConnectToExistingFirefox() {
212+
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
213+
214+
// Test connectToExisting method
215+
builder.connectToExisting(2829);
216+
GeckoDriverService service = builder.build();
217+
218+
assertThat(service).isNotNull();
219+
service.stop();
220+
}
221+
222+
@Test
223+
void geckoDriverServiceCustomWebSocketPort() {
224+
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
225+
226+
// Test withWebSocketPort method
227+
builder.withWebSocketPort(9225);
228+
GeckoDriverService service = builder.build();
229+
230+
assertThat(service).isNotNull();
231+
service.stop();
232+
}
209233
}

0 commit comments

Comments
 (0)