Skip to content

Commit 2a9b686

Browse files
committed
Adding some generics to DriverService.Builder
1 parent 7458198 commit 2a9b686

File tree

3 files changed

+187
-215
lines changed

3 files changed

+187
-215
lines changed

java/client/src/org/openqa/selenium/chrome/ChromeDriverService.java

Lines changed: 35 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,14 @@
1717

1818
package org.openqa.selenium.chrome;
1919

20-
import static com.google.common.base.Preconditions.checkArgument;
21-
import static com.google.common.base.Preconditions.checkNotNull;
22-
import static com.google.common.base.Preconditions.checkState;
23-
2420
import com.google.common.collect.ImmutableList;
2521
import com.google.common.collect.ImmutableMap;
2622

27-
import org.openqa.selenium.Beta;
2823
import org.openqa.selenium.WebDriverException;
29-
import org.openqa.selenium.net.PortProber;
3024
import org.openqa.selenium.remote.service.DriverService;
3125

3226
import java.io.File;
3327
import java.io.IOException;
34-
import java.util.Map;
3528

3629
/**
3730
* Manages the life and death of a chromedriver server.
@@ -86,87 +79,17 @@ public ChromeDriverService(File executable, int port, ImmutableList<String> args
8679
* @return A new ChromeDriverService using the default configuration.
8780
*/
8881
public static ChromeDriverService createDefaultService() {
89-
File exe = findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
90-
"http://code.google.com/p/selenium/wiki/ChromeDriver",
91-
"http://chromedriver.storage.googleapis.com/index.html");
92-
return new Builder().usingDriverExecutable(exe).usingAnyFreePort().build();
82+
return new Builder().usingAnyFreePort().build();
9383
}
9484

9585
/**
9686
* Builder used to configure new {@link ChromeDriverService} instances.
9787
*/
98-
public static class Builder {
88+
public static class Builder extends DriverService.Builder<ChromeDriverService> {
9989

100-
private int port = 0;
101-
private File exe = null;
102-
private ImmutableMap<String, String> environment = ImmutableMap.of();
103-
String chromeLogFile = System.getProperty(CHROME_DRIVER_LOG_PROPERTY);
104-
private File logFile = chromeLogFile == null ? null : new File(chromeLogFile);
10590
private boolean verbose = Boolean.getBoolean(CHROME_DRIVER_VERBOSE_LOG_PROPERTY);
10691
private boolean silent = Boolean.getBoolean(CHROME_DRIVER_SILENT_OUTPUT_PROPERTY);
10792

108-
/**
109-
* Sets which driver executable the builder will use.
110-
*
111-
* @param file The executable to use.
112-
* @return A self reference.
113-
*/
114-
public Builder usingDriverExecutable(File file) {
115-
checkNotNull(file);
116-
checkExecutable(file);
117-
this.exe = file;
118-
return this;
119-
}
120-
121-
/**
122-
* Sets which port the driver server should be started on. A value of 0 indicates that any
123-
* free port may be used.
124-
*
125-
* @param port The port to use; must be non-negative.
126-
* @return A self reference.
127-
*/
128-
public Builder usingPort(int port) {
129-
checkArgument(port >= 0, "Invalid port number: %d", port);
130-
this.port = port;
131-
return this;
132-
}
133-
134-
/**
135-
* Configures the driver server to start on any available port.
136-
*
137-
* @return A self reference.
138-
*/
139-
public Builder usingAnyFreePort() {
140-
this.port = 0;
141-
return this;
142-
}
143-
144-
/**
145-
* Defines the environment for the launched driver server. These
146-
* settings will be inherited by every browser session launched by the
147-
* server.
148-
*
149-
* @param environment A map of the environment variables to launch the
150-
* server with.
151-
* @return A self reference.
152-
*/
153-
@Beta
154-
public Builder withEnvironment(Map<String, String> environment) {
155-
this.environment = ImmutableMap.copyOf(environment);
156-
return this;
157-
}
158-
159-
/**
160-
* Configures the driver server to write log to the given file.
161-
*
162-
* @param logFile A file to write log to.
163-
* @return A self reference.
164-
*/
165-
public Builder withLogFile(File logFile) {
166-
this.logFile = logFile;
167-
return this;
168-
}
169-
17093
/**
17194
* Configures the driver server verbosity.
17295
*
@@ -189,34 +112,43 @@ public Builder withSilent(boolean silent) {
189112
return this;
190113
}
191114

192-
/**
193-
* Creates a new service to manage the driver server. Before creating a new service, the
194-
* builder will find a port for the server to listen to.
195-
*
196-
* @return The new service object.
197-
*/
198-
public ChromeDriverService build() {
199-
if (port == 0) {
200-
port = PortProber.findFreePort();
201-
}
202-
203-
checkState(exe != null, "Path to the driver executable not specified");
115+
@Override
116+
protected File findDefaultExecutable() {
117+
return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
118+
"http://code.google.com/p/selenium/wiki/ChromeDriver",
119+
"http://chromedriver.storage.googleapis.com/index.html");
120+
}
204121

205-
try {
206-
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
207-
argsBuilder.add(String.format("--port=%d", port));
208-
if (logFile != null) {
209-
argsBuilder.add(String.format("--log-path=%s", logFile.getAbsolutePath()));
210-
}
211-
if (verbose) {
212-
argsBuilder.add("--verbose");
213-
}
214-
if (silent) {
215-
argsBuilder.add("--silent");
122+
@Override
123+
protected ImmutableList<String> createArgs() {
124+
if (getLogFile() == null) {
125+
String logFilePath = System.getProperty(CHROME_DRIVER_LOG_PROPERTY);
126+
if (logFilePath != null) {
127+
withLogFile(new File(logFilePath));
216128
}
129+
}
217130

218-
return new ChromeDriverService(exe, port, argsBuilder.build(), environment);
131+
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
132+
argsBuilder.add(String.format("--port=%d", getPort()));
133+
if (getLogFile() != null) {
134+
argsBuilder.add(String.format("--log-path=%s", getLogFile().getAbsolutePath()));
135+
}
136+
if (verbose) {
137+
argsBuilder.add("--verbose");
138+
}
139+
if (silent) {
140+
argsBuilder.add("--silent");
141+
}
219142

143+
return argsBuilder.build();
144+
}
145+
146+
@Override
147+
protected ChromeDriverService createDriverService(File exe, int port,
148+
ImmutableList<String> args,
149+
ImmutableMap<String, String> environment) {
150+
try {
151+
return new ChromeDriverService(exe, port, args, environment);
220152
} catch (IOException e) {
221153
throw new WebDriverException(e);
222154
}

java/client/src/org/openqa/selenium/ie/InternetExplorerDriverService.java

Lines changed: 40 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,14 @@
1717

1818
package org.openqa.selenium.ie;
1919

20-
import static com.google.common.base.Preconditions.checkArgument;
21-
import static com.google.common.base.Preconditions.checkNotNull;
22-
2320
import com.google.common.collect.ImmutableList;
2421
import com.google.common.collect.ImmutableMap;
2522

26-
import org.openqa.selenium.Beta;
2723
import org.openqa.selenium.WebDriverException;
28-
import org.openqa.selenium.net.PortProber;
2924
import org.openqa.selenium.remote.service.DriverService;
3025

3126
import java.io.File;
3227
import java.io.IOException;
33-
import java.util.Map;
3428

3529
/**
3630
* Manages the life and death of an IEDriverServer.
@@ -102,12 +96,8 @@ public static InternetExplorerDriverService createDefaultService() {
10296
/**
10397
* Builder used to configure new {@link InternetExplorerDriverService} instances.
10498
*/
105-
public static class Builder {
99+
public static class Builder extends DriverService.Builder<InternetExplorerDriverService> {
106100

107-
private int port = 0;
108-
private File exe = null;
109-
private ImmutableMap<String, String> environment = ImmutableMap.of();
110-
private File logFile;
111101
private InternetExplorerDriverLogLevel logLevel;
112102
private InternetExplorerDriverEngine engineImplementation;
113103
private String host = null;
@@ -116,68 +106,6 @@ public static class Builder {
116106
private Boolean forceCreateProcess = null;
117107
private String ieSwitches = null;
118108

119-
/**
120-
* Sets which driver executable the builder will use.
121-
*
122-
* @param file The executable to use.
123-
* @return A self reference.
124-
*/
125-
public Builder usingDriverExecutable(File file) {
126-
checkNotNull(file);
127-
checkExecutable(file);
128-
this.exe = file;
129-
return this;
130-
}
131-
132-
/**
133-
* Sets which port the driver server should be started on. A value of 0 indicates that any
134-
* free port may be used.
135-
*
136-
* @param port The port to use; must be non-negative.
137-
* @return A self reference.
138-
*/
139-
public Builder usingPort(int port) {
140-
checkArgument(port >= 0, "Invalid port number: %d", port);
141-
this.port = port;
142-
return this;
143-
}
144-
145-
/**
146-
* Configures the driver server to start on any available port.
147-
*
148-
* @return A self reference.
149-
*/
150-
public Builder usingAnyFreePort() {
151-
this.port = 0;
152-
return this;
153-
}
154-
155-
/**
156-
* Defines the environment for the launched driver server. These
157-
* settings will be inherited by every browser session launched by the
158-
* server.
159-
*
160-
* @param environment A map of the environment variables to launch the
161-
* server with.
162-
* @return A self reference.
163-
*/
164-
@Beta
165-
public Builder withEnvironment(Map<String, String> environment) {
166-
this.environment = ImmutableMap.copyOf(environment);
167-
return this;
168-
}
169-
170-
/**
171-
* Configures the driver server to write log to the given file.
172-
*
173-
* @param logFile A file to write log to.
174-
* @return A self reference.
175-
*/
176-
public Builder withLogFile(File logFile) {
177-
this.logFile = logFile;
178-
return this;
179-
}
180-
181109
/**
182110
* Configures the logging level for the driver server.
183111
*
@@ -233,25 +161,19 @@ public Builder withSilent(Boolean silent) {
233161
return this;
234162
}
235163

236-
/**
237-
* Creates a new service to manage the driver server. Before creating a new service, the
238-
* builder will find a port for the server to listen to.
239-
*
240-
* @return The new service object.
241-
*/
242-
public InternetExplorerDriverService build() {
243-
if (port == 0) {
244-
port = PortProber.findFreePort();
245-
}
246-
if (exe == null) {
247-
exe = findExecutable("IEDriverServer", IE_DRIVER_EXE_PROPERTY,
248-
"http://code.google.com/p/selenium/wiki/InternetExplorerDriver",
249-
"http://selenium-release.storage.googleapis.com/index.html");
250-
}
251-
if (logFile == null) {
164+
@Override
165+
protected File findDefaultExecutable() {
166+
return findExecutable("IEDriverServer", IE_DRIVER_EXE_PROPERTY,
167+
"http://code.google.com/p/selenium/wiki/InternetExplorerDriver",
168+
"http://selenium-release.storage.googleapis.com/index.html");
169+
}
170+
171+
@Override
172+
protected ImmutableList<String> createArgs() {
173+
if (getLogFile() == null) {
252174
String logFilePath = System.getProperty(IE_DRIVER_LOGFILE_PROPERTY);
253175
if (logFilePath != null) {
254-
logFile = new File(logFilePath);
176+
withLogFile(new File(logFilePath));
255177
}
256178
}
257179
if (logLevel == null) {
@@ -285,30 +207,36 @@ public InternetExplorerDriverService build() {
285207
}
286208
}
287209

288-
try {
289-
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
290-
argsBuilder.add(String.format("--port=%d", port));
291-
if (logFile != null) {
292-
argsBuilder.add(String.format("--log-file=\"%s\"", logFile.getAbsolutePath()));
293-
}
294-
if (logLevel != null) {
295-
argsBuilder.add(String.format("--log-level=%s", logLevel.toString()));
296-
}
297-
if (engineImplementation != null) {
298-
argsBuilder.add(String.format("--implementation=%s", engineImplementation.toString()));
299-
}
300-
if (host != null) {
301-
argsBuilder.add(String.format("--host=%s", host));
302-
}
303-
if (extractPath != null) {
304-
argsBuilder.add(String.format("--extract-path=\"%s\"", extractPath.getAbsolutePath()));
305-
}
306-
if (silent != null && silent.equals(Boolean.TRUE)) {
307-
argsBuilder.add("--silent");
308-
}
210+
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
211+
argsBuilder.add(String.format("--port=%d", getPort()));
212+
if (getLogFile() != null) {
213+
argsBuilder.add(String.format("--log-file=\"%s\"", getLogFile().getAbsolutePath()));
214+
}
215+
if (logLevel != null) {
216+
argsBuilder.add(String.format("--log-level=%s", logLevel.toString()));
217+
}
218+
if (engineImplementation != null) {
219+
argsBuilder.add(String.format("--implementation=%s", engineImplementation.toString()));
220+
}
221+
if (host != null) {
222+
argsBuilder.add(String.format("--host=%s", host));
223+
}
224+
if (extractPath != null) {
225+
argsBuilder.add(String.format("--extract-path=\"%s\"", extractPath.getAbsolutePath()));
226+
}
227+
if (silent != null && silent.equals(Boolean.TRUE)) {
228+
argsBuilder.add("--silent");
229+
}
309230

310-
return new InternetExplorerDriverService(exe, port, argsBuilder.build(), environment);
231+
return argsBuilder.build();
232+
}
311233

234+
@Override
235+
protected InternetExplorerDriverService createDriverService(File exe, int port,
236+
ImmutableList<String> args,
237+
ImmutableMap<String, String> environment) {
238+
try {
239+
return new InternetExplorerDriverService(exe, port, args, environment);
312240
} catch (IOException e) {
313241
throw new WebDriverException(e);
314242
}

0 commit comments

Comments
 (0)