Selenium Firefox WebDriver implementation for automating Firefox browser interactions
—
GeckoDriver service lifecycle management including executable location, logging configuration, and process control. The service classes handle the communication between WebDriver and the Firefox browser.
Manages the GeckoDriver executable lifecycle and communication with Firefox browser.
/**
* Manages GeckoDriver executable lifecycle.
* Handles process startup, communication, and cleanup.
*/
public class GeckoDriverService extends DriverService {
/**
* The name of the GeckoDriver executable.
*/
public static final String GECKO_DRIVER_NAME = "geckodriver";
/**
* System property for specifying GeckoDriver executable path.
*/
public static final String GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver";
/**
* System property for specifying GeckoDriver log file path.
*/
public static final String GECKO_DRIVER_LOG_PROPERTY = "webdriver.firefox.logfile";
/**
* System property for specifying GeckoDriver log level.
*/
public static final String GECKO_DRIVER_LOG_LEVEL_PROPERTY = "webdriver.firefox.logLevel";
/**
* System property for disabling GeckoDriver log truncation.
*/
public static final String GECKO_DRIVER_LOG_NO_TRUNCATE = "webdriver.firefox.logTruncate";
/**
* System property for specifying Firefox profile root directory.
*/
public static final String GECKO_DRIVER_PROFILE_ROOT = "webdriver.firefox.profileRoot";
/**
* Creates a GeckoDriverService instance.
* @param executable Path to GeckoDriver executable
* @param port Port number for service communication
* @param timeout Timeout for service operations
* @param args Additional command line arguments
* @param environment Environment variables for the process
*/
public GeckoDriverService(File executable, int port, Duration timeout,
List<String> args, Map<String, String> environment);
}Usage Examples:
import org.openqa.selenium.firefox.GeckoDriverService;
import java.io.File;
import java.time.Duration;
import java.util.Arrays;
// Create service with custom executable
File geckoDriver = new File("/path/to/geckodriver");
GeckoDriverService service = new GeckoDriverService(
geckoDriver,
4444, // port
Duration.ofSeconds(60), // timeout
Arrays.asList("--log", "debug"), // args
System.getenv() // environment
);Convenient factory methods for creating service instances.
/**
* Returns the driver name for identification.
* @return "geckodriver"
*/
public String getDriverName();
/**
* Returns the system property name for driver executable.
* @return "webdriver.gecko.driver"
*/
public String getDriverProperty();
/**
* Returns default driver options for Firefox.
* @return FirefoxOptions with default configuration
*/
public Capabilities getDefaultDriverOptions();
/**
* Creates a GeckoDriverService with default configuration.
* Automatically locates GeckoDriver executable and uses default settings.
* @return GeckoDriverService instance with defaults
*/
public static GeckoDriverService createDefaultService();Usage Examples:
// Create default service (most common usage)
GeckoDriverService service = GeckoDriverService.createDefaultService();
WebDriver driver = new FirefoxDriver(service, options);
// Check service properties
System.out.println("Driver name: " + service.getDriverName());
System.out.println("Driver property: " + service.getDriverProperty());Builder pattern for creating customized GeckoDriverService instances.
/**
* Builder for creating GeckoDriverService instances with custom configuration.
*/
public static class Builder extends DriverService.Builder<GeckoDriverService, Builder> {
/**
* Scores how well this service matches the given capabilities.
* @param capabilities WebDriver capabilities to evaluate
* @return Score indicating compatibility (higher is better)
*/
public int score(Capabilities capabilities);
/**
* Sets allowed hosts for GeckoDriver connections.
* @param allowHosts Comma-separated list of allowed host names/IPs
* @return This builder instance for method chaining
*/
public Builder withAllowHosts(String allowHosts);
/**
* Sets the GeckoDriver log level.
* @param logLevel Log level from FirefoxDriverLogLevel enum
* @return This builder instance for method chaining
*/
public Builder withLogLevel(FirefoxDriverLogLevel logLevel);
/**
* Sets whether to truncate GeckoDriver logs.
* @param truncate true to truncate logs, false for full logs
* @return This builder instance for method chaining
*/
public Builder withTruncatedLogs(Boolean truncate);
/**
* Sets the Firefox profile root directory.
* @param root Directory for Firefox profile storage
* @return This builder instance for method chaining
*/
public Builder withProfileRoot(File root);
}Usage Examples:
import org.openqa.selenium.firefox.FirefoxDriverLogLevel;
import java.io.File;
// Create service with custom configuration using builder
GeckoDriverService service = new GeckoDriverService.Builder()
.usingDriverExecutable(new File("/usr/local/bin/geckodriver"))
.usingPort(4444)
.withTimeout(Duration.ofSeconds(90))
.withLogLevel(FirefoxDriverLogLevel.DEBUG)
.withTruncatedLogs(false)
.withAllowHosts("localhost,127.0.0.1")
.withProfileRoot(new File("/tmp/firefox-profiles"))
.build();
WebDriver driver = new FirefoxDriver(service, options);Base class for Firefox driver services providing common functionality.
/**
* Abstract base class for Firefox driver services.
* Provides common functionality for different Firefox service implementations.
*/
public abstract class FirefoxDriverService extends DriverService {
/**
* Creates a FirefoxDriverService instance.
* @param executable Path to driver executable
* @param port Port number for service communication
* @param timeout Timeout for service operations
* @param args Additional command line arguments
* @param environment Environment variables for the process
*/
public FirefoxDriverService(File executable, int port, Duration timeout,
List<String> args, Map<String, String> environment);
}Base builder for Firefox driver services.
/**
* Abstract builder for Firefox driver services.
* @param <DS> Driver service type
* @param <B> Builder type for self-returning methods
*/
public abstract static class Builder<DS extends FirefoxDriverService,
B extends FirefoxDriverService.Builder<?, ?>>
extends DriverService.Builder<DS, B> {
// Builder implementation details
}Complete Service Configuration Example:
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxDriverLogLevel;
import org.openqa.selenium.WebDriver;
import java.io.File;
import java.time.Duration;
// Configure comprehensive service with all options
GeckoDriverService service = new GeckoDriverService.Builder()
// Executable configuration
.usingDriverExecutable(new File("/usr/local/bin/geckodriver"))
.usingPort(4444)
.withTimeout(Duration.ofSeconds(120))
// Logging configuration
.withLogLevel(FirefoxDriverLogLevel.INFO)
.withTruncatedLogs(false)
.withLogFile(new File("/var/log/geckodriver.log"))
// Network configuration
.withAllowHosts("localhost,127.0.0.1,192.168.1.100")
// Profile configuration
.withProfileRoot(new File("/tmp/firefox-profiles"))
// Environment variables
.withEnvironment(Map.of(
"MOZ_HEADLESS", "1",
"DISPLAY", ":99"
))
.build();
// Verify service configuration
System.out.println("Service port: " + service.getUrl().getPort());
System.out.println("Driver name: " + service.getDriverName());
// Use service with driver
FirefoxOptions options = new FirefoxOptions().setHeadless(true);
WebDriver driver = new FirefoxDriver(service, options);
try {
driver.get("https://example.com");
// ... test operations
} finally {
driver.quit();
service.stop(); // Explicitly stop service if needed
}System Property Configuration:
// Alternative: Configure via system properties
System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,
"/usr/local/bin/geckodriver");
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY,
"/var/log/geckodriver.log");
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_LEVEL_PROPERTY,
"INFO");
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_NO_TRUNCATE,
"false");
// Create default service (will use system properties)
GeckoDriverService service = GeckoDriverService.createDefaultService();Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-firefox-driver