0
# Service Management
1
2
ChromeDriver server lifecycle management with port configuration, logging options, process control, and builder pattern for creating configured service instances in distributed testing environments.
3
4
## Capabilities
5
6
### ChromeDriverService Class
7
8
Manages the lifecycle of ChromeDriver server processes with configuration for executables, ports, logging, and security settings.
9
10
```java { .api }
11
/**
12
* Manages the life and death of a ChromeDriver server.
13
* Extends DriverService to provide Chrome-specific server management.
14
*/
15
public class ChromeDriverService extends DriverService {
16
17
/**
18
* System property that defines the location of the chromedriver executable.
19
*/
20
public static final String CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver";
21
22
/**
23
* System property that defines the location of the log file.
24
*/
25
public static final String CHROME_DRIVER_LOG_PROPERTY = "webdriver.chrome.logfile";
26
27
/**
28
* Boolean system property that defines whether chromedriver should start with verbose logging.
29
*/
30
public static final String CHROME_DRIVER_VERBOSE_LOG_PROPERTY = "webdriver.chrome.verboseLogging";
31
32
/**
33
* Boolean system property that defines whether chromedriver should start in silent mode.
34
*/
35
public static final String CHROME_DRIVER_SILENT_OUTPUT_PROPERTY = "webdriver.chrome.silentOutput";
36
37
/**
38
* System property that defines comma-separated list of remote IPv4 addresses
39
* which are allowed to connect to ChromeDriver.
40
*/
41
public static final String CHROME_DRIVER_WHITELISTED_IPS_PROPERTY = "webdriver.chrome.whitelistedIps";
42
43
/**
44
* Creates a ChromeDriverService with specified configuration.
45
* @param executable The chromedriver executable file
46
* @param port Which port to start the ChromeDriver on
47
* @param args The arguments to the launched server
48
* @param environment The environment for the launched server
49
* @throws IOException If an I/O error occurs
50
*/
51
public ChromeDriverService(
52
File executable,
53
int port,
54
ImmutableList<String> args,
55
ImmutableMap<String, String> environment) throws IOException;
56
}
57
```
58
59
### Default Service Creation
60
61
Create ChromeDriverService instances with default configuration using system properties.
62
63
```java { .api }
64
/**
65
* Configures and returns a new ChromeDriverService using the default configuration.
66
* Uses the chromedriver executable identified by the CHROME_DRIVER_EXE_PROPERTY system property.
67
* Each service created by this method will be configured to use a free port on the current system.
68
* @return A new ChromeDriverService using the default configuration
69
*/
70
public static ChromeDriverService createDefaultService();
71
```
72
73
**Usage Example:**
74
75
```java
76
import org.openqa.selenium.chrome.ChromeDriverService;
77
78
// Set system property for chromedriver location
79
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
80
81
// Create default service - will find free port automatically
82
ChromeDriverService service = ChromeDriverService.createDefaultService();
83
84
// Start the service
85
service.start();
86
87
// Use with RemoteWebDriver for better resource management
88
WebDriver driver = new RemoteWebDriver(service.getUrl(), new ChromeOptions());
89
90
// Clean up
91
driver.quit();
92
service.stop();
93
```
94
95
### Service Builder
96
97
Builder pattern for creating customized ChromeDriverService instances with specific configurations.
98
99
```java { .api }
100
/**
101
* Builder used to configure new ChromeDriverService instances.
102
* Provides fluent API for service configuration.
103
*/
104
public static class Builder extends DriverService.Builder<ChromeDriverService, ChromeDriverService.Builder> {
105
106
/**
107
* Scores the given capabilities to determine if this service supports them.
108
* Higher scores indicate better support.
109
* @param capabilities The capabilities to score
110
* @return Score indicating support level (0 = no support, higher = better support)
111
*/
112
public int score(Capabilities capabilities);
113
114
/**
115
* Sets which driver executable the builder will use.
116
* @param file The chromedriver executable to use
117
* @return A self reference for method chaining
118
*/
119
public Builder usingDriverExecutable(File file);
120
121
/**
122
* Sets which port the driver server should be started on.
123
* @param port The port to use; must be non-negative
124
* @return A self reference for method chaining
125
*/
126
public Builder usingPort(int port);
127
128
/**
129
* Configures the driver server to start on any available port.
130
* @return A self reference for method chaining
131
*/
132
public Builder usingAnyFreePort();
133
134
/**
135
* Configures the driver server to write log to the given file.
136
* @param logFile The file to write logs to
137
* @return A self reference for method chaining
138
*/
139
public Builder withLogFile(File logFile);
140
141
/**
142
* Defines the environment variables for the launched driver server.
143
* @param environment A map of environment variables to launch the server with
144
* @return A self reference for method chaining
145
*/
146
public Builder withEnvironment(Map<String, String> environment);
147
148
/**
149
* Configures the driver server verbosity.
150
* @param verbose True for verbose output, false otherwise
151
* @return A self reference for method chaining
152
*/
153
public Builder withVerbose(boolean verbose);
154
155
/**
156
* Configures the driver server for silent output.
157
* @param silent True for silent output, false otherwise
158
* @return A self reference for method chaining
159
*/
160
public Builder withSilent(boolean silent);
161
162
/**
163
* Configures the comma-separated list of remote IPv4 addresses which are allowed
164
* to connect to the driver server.
165
* @param whitelistedIps Comma-separated list of remote IPv4 addresses
166
* @return A self reference for method chaining
167
*/
168
public Builder withWhitelistedIps(String whitelistedIps);
169
}
170
```
171
172
**Usage Examples:**
173
174
```java
175
import org.openqa.selenium.chrome.ChromeDriverService;
176
import java.io.File;
177
178
// Basic builder usage
179
ChromeDriverService service = new ChromeDriverService.Builder()
180
.usingDriverExecutable(new File("/path/to/chromedriver"))
181
.usingAnyFreePort()
182
.build();
183
184
// Advanced configuration
185
ChromeDriverService service = new ChromeDriverService.Builder()
186
.usingDriverExecutable(new File("/usr/local/bin/chromedriver"))
187
.usingPort(4444)
188
.withVerbose(true)
189
.withLogFile(new File("/tmp/chromedriver.log"))
190
.withWhitelistedIps("127.0.0.1,192.168.1.100")
191
.build();
192
193
// For CI/CD environments
194
ChromeDriverService service = new ChromeDriverService.Builder()
195
.usingAnyFreePort()
196
.withSilent(true)
197
.withLogFile(new File("./logs/chromedriver.log"))
198
.build();
199
200
service.start();
201
```
202
203
### Logging Configuration
204
205
Configure ChromeDriver server logging behavior for debugging and monitoring.
206
207
**System Properties:**
208
209
```java
210
// Set chromedriver executable location
211
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
212
213
// Set log file location
214
System.setProperty("webdriver.chrome.logfile", "/path/to/chromedriver.log");
215
216
// Enable verbose logging
217
System.setProperty("webdriver.chrome.verboseLogging", "true");
218
219
// Enable silent mode (minimal output)
220
System.setProperty("webdriver.chrome.silentOutput", "true");
221
222
// Set whitelisted IPs for remote connections
223
System.setProperty("webdriver.chrome.whitelistedIps", "127.0.0.1,192.168.1.0/24");
224
```
225
226
**Builder Configuration:**
227
228
```java
229
ChromeDriverService service = new ChromeDriverService.Builder()
230
.withVerbose(true) // Enable verbose logging
231
.withSilent(false) // Disable silent mode
232
.withLogFile(new File("/tmp/chromedriver.log")) // Set log file
233
.build();
234
```
235
236
### Security Configuration
237
238
Configure IP whitelisting for secure remote connections to ChromeDriver server.
239
240
**Usage Example:**
241
242
```java
243
// Allow specific IPs to connect
244
ChromeDriverService service = new ChromeDriverService.Builder()
245
.withWhitelistedIps("127.0.0.1,192.168.1.100,10.0.0.0/8")
246
.usingPort(4444)
247
.build();
248
249
// Start service
250
service.start();
251
System.out.println("ChromeDriver server URL: " + service.getUrl());
252
253
// Service will only accept connections from whitelisted IPs
254
```
255
256
### Service Lifecycle Management
257
258
Manage ChromeDriver server startup, shutdown, and resource cleanup.
259
260
**Complete Example:**
261
262
```java
263
import org.openqa.selenium.chrome.ChromeDriverService;
264
import org.openqa.selenium.chrome.ChromeOptions;
265
import org.openqa.selenium.remote.RemoteWebDriver;
266
import org.openqa.selenium.WebDriver;
267
import java.io.File;
268
import java.io.IOException;
269
270
public class ChromeServiceExample {
271
private ChromeDriverService service;
272
273
public void setupService() throws IOException {
274
// Create and configure service
275
service = new ChromeDriverService.Builder()
276
.usingDriverExecutable(new File("/usr/local/bin/chromedriver"))
277
.usingAnyFreePort()
278
.withVerbose(true)
279
.withLogFile(new File("./chromedriver.log"))
280
.build();
281
282
// Start the service
283
service.start();
284
System.out.println("ChromeDriver started on: " + service.getUrl());
285
}
286
287
public WebDriver createDriver() {
288
// Create driver connected to running service
289
ChromeOptions options = new ChromeOptions();
290
options.setHeadless(true);
291
292
return new RemoteWebDriver(service.getUrl(), options);
293
}
294
295
public void teardownService() {
296
// Stop the service and clean up resources
297
if (service != null && service.isRunning()) {
298
service.stop();
299
System.out.println("ChromeDriver service stopped");
300
}
301
}
302
}
303
304
// Usage in test framework
305
ChromeServiceExample example = new ChromeServiceExample();
306
example.setupService();
307
308
WebDriver driver = example.createDriver();
309
driver.get("https://example.com");
310
System.out.println("Page title: " + driver.getTitle());
311
312
driver.quit();
313
example.teardownService();
314
```
315
316
### Distributed Testing Setup
317
318
Configure ChromeDriverService for Selenium Grid and distributed testing environments.
319
320
**Hub Registration Example:**
321
322
```java
323
// Start ChromeDriver service for Grid registration
324
ChromeDriverService service = new ChromeDriverService.Builder()
325
.usingDriverExecutable(new File("/opt/chromedriver"))
326
.usingPort(5555)
327
.withWhitelistedIps("0.0.0.0/0") // Allow all IPs for Grid
328
.withVerbose(false)
329
.build();
330
331
service.start();
332
333
// Service can now be registered with Selenium Grid hub
334
// Grid will route Chrome test requests to this service
335
```
336
337
### Environment Variables Integration
338
339
Integrate with environment-based configuration for container deployments.
340
341
**Usage Example:**
342
343
```java
344
public class EnvironmentAwareService {
345
public static ChromeDriverService createFromEnvironment() {
346
ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
347
348
// Use environment variables if available
349
String driverPath = System.getenv("CHROME_DRIVER_PATH");
350
if (driverPath != null) {
351
builder.usingDriverExecutable(new File(driverPath));
352
}
353
354
String port = System.getenv("CHROME_DRIVER_PORT");
355
if (port != null) {
356
builder.usingPort(Integer.parseInt(port));
357
} else {
358
builder.usingAnyFreePort();
359
}
360
361
// Configure based on environment
362
String verbose = System.getenv("CHROME_DRIVER_VERBOSE");
363
if ("true".equals(verbose)) {
364
builder.withVerbose(true);
365
}
366
367
String whitelistedIps = System.getenv("CHROME_DRIVER_WHITELIST");
368
if (whitelistedIps != null) {
369
builder.withWhitelistedIps(whitelistedIps);
370
}
371
372
return builder.build();
373
}
374
}
375
```
376
377
## Types
378
379
```java { .api }
380
// Guava collections used in service configuration
381
class ImmutableList<T> {
382
public static <T> ImmutableList<T> of(T... elements);
383
public static <T> Builder<T> builder();
384
}
385
386
class ImmutableMap<K, V> {
387
public static <K, V> ImmutableMap<K, V> of();
388
public static <K, V> Builder<K, V> builder();
389
}
390
391
// Base service class
392
abstract class DriverService {
393
public abstract void start() throws IOException;
394
public abstract void stop();
395
public abstract boolean isRunning();
396
public abstract URL getUrl();
397
398
public static abstract class Builder<DS extends DriverService, B extends Builder<?, ?>> {
399
public B usingDriverExecutable(File file);
400
public B usingAnyFreePort();
401
public B usingPort(int port);
402
public B withLogFile(File logFile);
403
public B withEnvironment(Map<String, String> environment);
404
public abstract DS build();
405
}
406
}
407
```