Skip to content

[java]: add websocket-port test and --connect-existing check #15462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions java/src/org/openqa/selenium/firefox/GeckoDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,26 @@ protected List<String> createArgs() {
List<String> args = new ArrayList<>();
args.add(String.format(Locale.ROOT, "--port=%d", getPort()));

int wsPort = PortProber.findFreePort();
args.add(String.format("--websocket-port=%d", wsPort));
// Check if we're connecting to an existing Firefox instance
boolean connectExisting = false;
for (String arg : args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be for (String arg : this.args) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joerg1985 this.args is not accessible inside createArgs(), is there any other way to access the passed args?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry my fault, this.getArgs() should work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even that doesn't works, I think we need to add a new method to the builder as Titus suggested.

if (arg.contains("--connect-existing")) {
connectExisting = true;
break;
}
}

args.add("--allow-origins");
args.add(String.format("http://127.0.0.1:%d", wsPort));
args.add(String.format("http://localhost:%d", wsPort));
args.add(String.format("http://[::1]:%d", wsPort));
// Only allocate a free port for the websocket when not connecting to an existing instance
// This avoids conflicts when multiple Firefox instances are started
if (!connectExisting) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO we should check for "--websocket-port" here too, someone might want to select a specific port to forward it:
mozilla/geckodriver#2218 (comment)

int wsPort = PortProber.findFreePort();
args.add(String.format("--websocket-port=%d", wsPort));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be two args like this and 0 will autoselect a free port:

args.add("--websocket-port");
args.add("0");


args.add("--allow-origins");
args.add(String.format("http://127.0.0.1:%d", wsPort));
args.add(String.format("http://localhost:%d", wsPort));
args.add(String.format("http://[::1]:%d", wsPort));
}

if (logLevel != null) {
args.add("--log");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openqa.selenium.ParallelTestRunner.Worker;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

Expand Down Expand Up @@ -164,4 +165,45 @@ void shouldBeAbleToUseTheSameProfileMoreThanOnce() {
if (two != null) two.quit();
}
}

@Test
void multipleFirefoxInstancesWithBiDiEnabledCanRunSimultaneously() {
// Create two Firefox instances with BiDi enabled
FirefoxOptions options1 = new FirefoxOptions().enableBiDi();
FirefoxOptions options2 = new FirefoxOptions().enableBiDi();

WebDriver driver1 = null;
WebDriver driver2 = null;

try {
driver1 = new WebDriverBuilder().get(options1);
BiDi biDi1 = ((FirefoxDriver) driver1).getBiDi();
assertThat(biDi1).isNotNull();

// Extract the BiDi websocket URL and port for the first instance
String webSocketUrl1 =
(String) ((FirefoxDriver) driver1).getCapabilities().getCapability("webSocketUrl");
String port1 = webSocketUrl1.replaceAll("^ws://[^:]+:(\\d+)/.*$", "$1");

driver2 = new WebDriverBuilder().get(options2);
BiDi biDi2 = ((FirefoxDriver) driver2).getBiDi();
assertThat(biDi2).isNotNull();

// Extract the BiDi websocket URL and port for the second instance
String webSocketUrl2 =
(String) ((FirefoxDriver) driver2).getCapabilities().getCapability("webSocketUrl");
String port2 = webSocketUrl2.replaceAll("^ws://[^:]+:(\\d+)/.*$", "$1");

// Verify that the ports are different
assertThat(port1).isNotEqualTo(port2);
} finally {
// Clean up
if (driver1 != null) {
driver1.quit();
}
if (driver2 != null) {
driver2.quit();
}
}
}
}
Loading