Skip to content

Commit 413a130

Browse files
committed
add check for `--connect-existing
1 parent 05ab017 commit 413a130

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,26 @@ protected List<String> createArgs() {
222222
List<String> args = new ArrayList<>();
223223
args.add(String.format(Locale.ROOT, "--port=%d", getPort()));
224224

225-
int wsPort = PortProber.findFreePort();
226-
args.add(String.format("--websocket-port=%d", wsPort));
225+
// Check if we're connecting to an existing Firefox instance
226+
boolean connectExisting = false;
227+
for (String arg : args) {
228+
if (arg.contains("--connect-existing")) {
229+
connectExisting = true;
230+
break;
231+
}
232+
}
227233

228-
args.add("--allow-origins");
229-
args.add(String.format("http://127.0.0.1:%d", wsPort));
230-
args.add(String.format("http://localhost:%d", wsPort));
231-
args.add(String.format("http://[::1]:%d", wsPort));
234+
// Only allocate a free port for the websocket when not connecting to an existing instance
235+
// This avoids conflicts when multiple Firefox instances are started
236+
if (!connectExisting) {
237+
int wsPort = PortProber.findFreePort();
238+
args.add(String.format("--websocket-port=%d", wsPort));
239+
240+
args.add("--allow-origins");
241+
args.add(String.format("http://127.0.0.1:%d", wsPort));
242+
args.add(String.format("http://localhost:%d", wsPort));
243+
args.add(String.format("http://[::1]:%d", wsPort));
244+
}
232245

233246
if (logLevel != null) {
234247
args.add("--log");

0 commit comments

Comments
 (0)