0
# Service Management
1
2
GeckoDriver service lifecycle management including executable location, logging configuration, and process control. The service classes handle the communication between WebDriver and the Firefox browser.
3
4
## Capabilities
5
6
### GeckoDriverService Class
7
8
Manages the GeckoDriver executable lifecycle and communication with Firefox browser.
9
10
```java { .api }
11
/**
12
* Manages GeckoDriver executable lifecycle.
13
* Handles process startup, communication, and cleanup.
14
*/
15
public class GeckoDriverService extends DriverService {
16
17
/**
18
* The name of the GeckoDriver executable.
19
*/
20
public static final String GECKO_DRIVER_NAME = "geckodriver";
21
22
/**
23
* System property for specifying GeckoDriver executable path.
24
*/
25
public static final String GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver";
26
27
/**
28
* System property for specifying GeckoDriver log file path.
29
*/
30
public static final String GECKO_DRIVER_LOG_PROPERTY = "webdriver.firefox.logfile";
31
32
/**
33
* System property for specifying GeckoDriver log level.
34
*/
35
public static final String GECKO_DRIVER_LOG_LEVEL_PROPERTY = "webdriver.firefox.logLevel";
36
37
/**
38
* System property for disabling GeckoDriver log truncation.
39
*/
40
public static final String GECKO_DRIVER_LOG_NO_TRUNCATE = "webdriver.firefox.logTruncate";
41
42
/**
43
* System property for specifying Firefox profile root directory.
44
*/
45
public static final String GECKO_DRIVER_PROFILE_ROOT = "webdriver.firefox.profileRoot";
46
47
/**
48
* Creates a GeckoDriverService instance.
49
* @param executable Path to GeckoDriver executable
50
* @param port Port number for service communication
51
* @param timeout Timeout for service operations
52
* @param args Additional command line arguments
53
* @param environment Environment variables for the process
54
*/
55
public GeckoDriverService(File executable, int port, Duration timeout,
56
List<String> args, Map<String, String> environment);
57
}
58
```
59
60
**Usage Examples:**
61
62
```java
63
import org.openqa.selenium.firefox.GeckoDriverService;
64
import java.io.File;
65
import java.time.Duration;
66
import java.util.Arrays;
67
68
// Create service with custom executable
69
File geckoDriver = new File("/path/to/geckodriver");
70
GeckoDriverService service = new GeckoDriverService(
71
geckoDriver,
72
4444, // port
73
Duration.ofSeconds(60), // timeout
74
Arrays.asList("--log", "debug"), // args
75
System.getenv() // environment
76
);
77
```
78
79
### Service Factory Methods
80
81
Convenient factory methods for creating service instances.
82
83
```java { .api }
84
/**
85
* Returns the driver name for identification.
86
* @return "geckodriver"
87
*/
88
public String getDriverName();
89
90
/**
91
* Returns the system property name for driver executable.
92
* @return "webdriver.gecko.driver"
93
*/
94
public String getDriverProperty();
95
96
/**
97
* Returns default driver options for Firefox.
98
* @return FirefoxOptions with default configuration
99
*/
100
public Capabilities getDefaultDriverOptions();
101
102
/**
103
* Creates a GeckoDriverService with default configuration.
104
* Automatically locates GeckoDriver executable and uses default settings.
105
* @return GeckoDriverService instance with defaults
106
*/
107
public static GeckoDriverService createDefaultService();
108
```
109
110
**Usage Examples:**
111
112
```java
113
// Create default service (most common usage)
114
GeckoDriverService service = GeckoDriverService.createDefaultService();
115
WebDriver driver = new FirefoxDriver(service, options);
116
117
// Check service properties
118
System.out.println("Driver name: " + service.getDriverName());
119
System.out.println("Driver property: " + service.getDriverProperty());
120
```
121
122
### GeckoDriverService Builder
123
124
Builder pattern for creating customized GeckoDriverService instances.
125
126
```java { .api }
127
/**
128
* Builder for creating GeckoDriverService instances with custom configuration.
129
*/
130
public static class Builder extends DriverService.Builder<GeckoDriverService, Builder> {
131
132
/**
133
* Scores how well this service matches the given capabilities.
134
* @param capabilities WebDriver capabilities to evaluate
135
* @return Score indicating compatibility (higher is better)
136
*/
137
public int score(Capabilities capabilities);
138
139
/**
140
* Sets allowed hosts for GeckoDriver connections.
141
* @param allowHosts Comma-separated list of allowed host names/IPs
142
* @return This builder instance for method chaining
143
*/
144
public Builder withAllowHosts(String allowHosts);
145
146
/**
147
* Sets the GeckoDriver log level.
148
* @param logLevel Log level from FirefoxDriverLogLevel enum
149
* @return This builder instance for method chaining
150
*/
151
public Builder withLogLevel(FirefoxDriverLogLevel logLevel);
152
153
/**
154
* Sets whether to truncate GeckoDriver logs.
155
* @param truncate true to truncate logs, false for full logs
156
* @return This builder instance for method chaining
157
*/
158
public Builder withTruncatedLogs(Boolean truncate);
159
160
/**
161
* Sets the Firefox profile root directory.
162
* @param root Directory for Firefox profile storage
163
* @return This builder instance for method chaining
164
*/
165
public Builder withProfileRoot(File root);
166
}
167
```
168
169
**Usage Examples:**
170
171
```java
172
import org.openqa.selenium.firefox.FirefoxDriverLogLevel;
173
import java.io.File;
174
175
// Create service with custom configuration using builder
176
GeckoDriverService service = new GeckoDriverService.Builder()
177
.usingDriverExecutable(new File("/usr/local/bin/geckodriver"))
178
.usingPort(4444)
179
.withTimeout(Duration.ofSeconds(90))
180
.withLogLevel(FirefoxDriverLogLevel.DEBUG)
181
.withTruncatedLogs(false)
182
.withAllowHosts("localhost,127.0.0.1")
183
.withProfileRoot(new File("/tmp/firefox-profiles"))
184
.build();
185
186
WebDriver driver = new FirefoxDriver(service, options);
187
```
188
189
### FirefoxDriverService Abstract Class
190
191
Base class for Firefox driver services providing common functionality.
192
193
```java { .api }
194
/**
195
* Abstract base class for Firefox driver services.
196
* Provides common functionality for different Firefox service implementations.
197
*/
198
public abstract class FirefoxDriverService extends DriverService {
199
200
/**
201
* Creates a FirefoxDriverService instance.
202
* @param executable Path to driver executable
203
* @param port Port number for service communication
204
* @param timeout Timeout for service operations
205
* @param args Additional command line arguments
206
* @param environment Environment variables for the process
207
*/
208
public FirefoxDriverService(File executable, int port, Duration timeout,
209
List<String> args, Map<String, String> environment);
210
}
211
```
212
213
### Abstract Builder Pattern
214
215
Base builder for Firefox driver services.
216
217
```java { .api }
218
/**
219
* Abstract builder for Firefox driver services.
220
* @param <DS> Driver service type
221
* @param <B> Builder type for self-returning methods
222
*/
223
public abstract static class Builder<DS extends FirefoxDriverService,
224
B extends FirefoxDriverService.Builder<?, ?>>
225
extends DriverService.Builder<DS, B> {
226
// Builder implementation details
227
}
228
```
229
230
**Complete Service Configuration Example:**
231
232
```java
233
import org.openqa.selenium.firefox.GeckoDriverService;
234
import org.openqa.selenium.firefox.FirefoxOptions;
235
import org.openqa.selenium.firefox.FirefoxDriverLogLevel;
236
import org.openqa.selenium.WebDriver;
237
import java.io.File;
238
import java.time.Duration;
239
240
// Configure comprehensive service with all options
241
GeckoDriverService service = new GeckoDriverService.Builder()
242
// Executable configuration
243
.usingDriverExecutable(new File("/usr/local/bin/geckodriver"))
244
.usingPort(4444)
245
.withTimeout(Duration.ofSeconds(120))
246
247
// Logging configuration
248
.withLogLevel(FirefoxDriverLogLevel.INFO)
249
.withTruncatedLogs(false)
250
.withLogFile(new File("/var/log/geckodriver.log"))
251
252
// Network configuration
253
.withAllowHosts("localhost,127.0.0.1,192.168.1.100")
254
255
// Profile configuration
256
.withProfileRoot(new File("/tmp/firefox-profiles"))
257
258
// Environment variables
259
.withEnvironment(Map.of(
260
"MOZ_HEADLESS", "1",
261
"DISPLAY", ":99"
262
))
263
264
.build();
265
266
// Verify service configuration
267
System.out.println("Service port: " + service.getUrl().getPort());
268
System.out.println("Driver name: " + service.getDriverName());
269
270
// Use service with driver
271
FirefoxOptions options = new FirefoxOptions().setHeadless(true);
272
WebDriver driver = new FirefoxDriver(service, options);
273
274
try {
275
driver.get("https://example.com");
276
// ... test operations
277
} finally {
278
driver.quit();
279
service.stop(); // Explicitly stop service if needed
280
}
281
```
282
283
**System Property Configuration:**
284
285
```java
286
// Alternative: Configure via system properties
287
System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,
288
"/usr/local/bin/geckodriver");
289
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY,
290
"/var/log/geckodriver.log");
291
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_LEVEL_PROPERTY,
292
"INFO");
293
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_NO_TRUNCATE,
294
"false");
295
296
// Create default service (will use system properties)
297
GeckoDriverService service = GeckoDriverService.createDefaultService();
298
```