Launching Chrome Browser Processes

Ravi Chandola
Javarevisited
Published in
4 min readApr 14, 2023
Photo by Growtika on Unsplash

Launching a custom Chrome browser with ProcessBuilder can be useful in situations where you need to test a web application on a version of Chrome that is not natively supported by Selenium WebDriver or that has been modified for some specific purpose, such as running on a particular operating system or device.

By using ProcessBuilder to launch the custom Chrome process, you can configure the browser with the desired options and then create a new instance of the ChromeDriver class that communicates with the custom browser process. This allows you to test your web application in a more realistic environment that more closely resembles the actual production environment.

For example, if your web application is designed to be used on a Chrome browser that has been customized to run on a specific version of the Linux operating system, you can use ProcessBuilder to launch the custom Chrome process on a Linux machine and then run your tests using the ChromeDriver class with appropriate options. This ensures that your tests are running in an environment that is as close to the actual production environment as possible, and helps you identify any compatibility issues or bugs that might arise in this specific environment.

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class CustomChromeLauncher {

public static void main(String[] args) throws IOException {

// Set the path to your custom Chrome binary
String chromeBinary = "C:\\drivers\\chrome\\chromedriver.exe";

// Set an additional command-line options you want to use
List<String> commandOptions = new ArrayList<>();
commandOptions.add("--headless"); // Example option

// Create the ProcessBuilder instance and set the command to launch Chrome with the desired options
ProcessBuilder pb = new ProcessBuilder();
List<String> command = new ArrayList<>();
command.add(chromeBinary);
command.addAll(commandOptions);
pb.command(command);

// Start the custom Chrome process
Process chromeProcess = pb.start();

// Create a new ChromeDriver instance that communicates with the custom browser process
WebDriver driver = new ChromeDriver(new ChromeOptions().setExperimentalOption("debuggerAddress", "localhost:9222"));

// Perform your Selenium tests using the ChromeDriver instance as usual
driver.get("https://www.google.com");
System.out.println("Page title: " + driver.getTitle());

// When you're done, quit the driver and terminate the custom Chrome process
driver.quit();
chromeProcess.destroy();
}
}

we first set the path to the custom Chrome binary and any additional command-line options we want to use in separate variables. Then, we create a new ProcessBuilder instance and set the command to launch Chrome with the desired options.

After starting the custom Chrome process with pb.start(), we create a new ChromeDriver instance that communicates with the custom browser process using the setExperimentalOption() method. We can then perform our Selenium tests using the driver object as usual, in this case navigating to Google and printing the page title.

Finally, we quit the driver and terminate the custom Chrome process by calling driver.quit() and chromeProcess.destroy(), respectively.

The setExperimentalOption() method in Selenium is used to set experimental options for the ChromeDriver instance. These experimental options are not yet fully supported by the ChromeDriver or the underlying browser, but can be used to enable new features or workarounds for known issues.

In the context of launching a custom Chrome browser with ProcessBuilder, we can use the setExperimentalOption() method to specify the address of the remote debugging protocol that the ChromeDriver should use to communicate with the custom browser process. This is necessary because the custom Chrome binary may not have the necessary flags or settings to enable communication with the ChromeDriver out of the box.

For example, in the following code snippet, we set the experimental option “debuggerAddress” to “localhost:9222”, which specifies that the ChromeDriver should connect to a debugging endpoint at “localhost:9222” in order to communicate with the custom browser process:

By using setExperimentalOption() to specify the remote debugging endpoint, we can ensure that the ChromeDriver instance communicates with the correct custom browser process, even if it is running on a different machine or has a different set of flags or settings.

setExperimentalOption()

In the context of Chrome browser automation with Selenium, the “debugger address” refers to a debugging endpoint that the Chrome browser exposes for remote debugging purposes.

When you start the Chrome browser with remote debugging enabled, it opens a WebSocket endpoint at a specific address (usually localhost:9222) that allows clients like Selenium to communicate with the browser and automate it.

By specifying the “debuggerAddress” experimental option in the ChromeOptions object passed to the ChromeDriver constructor, we can tell the ChromeDriver which debugger address to connect to when communicating with the browser.

new ChromeOptions().setExperimentalOption("debuggerAddress", "localhost:9222")

By connecting to the correct debugger address, the ChromeDriver can send commands to the Chrome browser and receive responses, allowing us to automate the browser using Selenium.

When using a custom Chrome binary with ProcessBuilder, you'll need to ensure that the binary is started with remote debugging enabled, and that it binds to a debugger endpoint that is accessible to the Selenium client. You can do this by passing the --remote-debugging-port flag to the Chrome binary when starting it up, along with the desired debugger address.

--

--

Ravi Chandola
Javarevisited

As a technology enthusiast, I have a passion for learning and sharing my knowledge with others. https://www.linkedin.com/in/ravi-chandola-304522133