-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
GeckoDriverService.java
216 lines (187 loc) · 7.31 KB
/
GeckoDriverService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.firefox;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
//import org.apache.commons.io.output.NullOutputStream;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.service.DriverService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* Manages the life and death of an GeckoDriver aka 'wires'.
*/
public class GeckoDriverService extends DriverService {
/**
* System property that defines the location of the GeckoDriver executable
* that will be used by the {@link #createDefaultService() default service}.
*/
public static final String GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver";
/**
*
* @param executable The GeckoDriver executable.
* @param port Which port to start the GeckoDriver on.
* @param args The arguments to the launched server.
* @param environment The environment for the launched server.
* @throws IOException If an I/O error occurs.
*/
public GeckoDriverService(
File executable, int port,
ImmutableList<String> args,
ImmutableMap<String, String> environment) throws IOException {
super(executable, port, args, environment);
}
/**
* Configures and returns a new {@link GeckoDriverService} using the default configuration. In
* this configuration, the service will use the GeckoDriver executable identified by the
* {@link #GECKO_DRIVER_EXE_PROPERTY} system property. Each service created by this method will
* be configured to use a free port on the current system.
*
* @return A new GeckoDriverService using the default configuration.
*/
public static GeckoDriverService createDefaultService() {
return new Builder().build();
}
static GeckoDriverService createDefaultService(Capabilities caps) {
Builder builder = new Builder();
Object binary = caps.getCapability(FirefoxDriver.BINARY);
if (binary != null) {
FirefoxBinary actualBinary;
if (binary instanceof FirefoxBinary) {
actualBinary = (FirefoxBinary) binary;
} else if (binary instanceof String) {
actualBinary = new FirefoxBinary(new File(String.valueOf(binary)));
} else {
throw new IllegalArgumentException(
"Expected binary to be a string or a binary: " + binary);
}
builder.usingFirefoxBinary(actualBinary);
}
return new Builder().build();
}
@Override
protected void waitUntilAvailable() throws MalformedURLException {
PortProber.waitForPortUp(getUrl().getPort(), 20, SECONDS);
}
@Override
protected boolean hasShutdownEndpoint() {
return false;
}
/**
* Builder used to configure new {@link GeckoDriverService} instances.
*/
@AutoService(DriverService.Builder.class)
public static class Builder extends DriverService.Builder<
GeckoDriverService, GeckoDriverService.Builder> {
private FirefoxBinary firefoxBinary;
public Builder() {
}
/**
* @param binary - A custom location where the Firefox binary is available.
*
* @deprecated Use method usingFirefoxBinary instead
*/
@Deprecated
public Builder(FirefoxBinary binary) {
this.firefoxBinary = binary;
}
@Override
public int score(Capabilities capabilites) {
int score = 0;
if (capabilites.getCapability(FirefoxDriver.MARIONETTE) == Boolean.FALSE) {
return 0; // We're not meant for this one.
}
if (BrowserType.FIREFOX.equals(capabilites.getBrowserName())) {
score++;
}
if (capabilites.getCapability(FirefoxOptions.FIREFOX_OPTIONS) != null) {
score++;
}
// This is the legacy firefox driver that they've asked for.
return score;
}
/**
* Sets which browser executable the builder will use.
*
* @param firefoxBinary The browser executable to use.
* @return A self reference.
*/
public Builder usingFirefoxBinary(FirefoxBinary firefoxBinary) {
checkNotNull(firefoxBinary);
checkExecutable(firefoxBinary.getFile());
this.firefoxBinary = firefoxBinary;
return this;
}
@Override
protected File findDefaultExecutable() {
return findExecutable(
"geckodriver", GECKO_DRIVER_EXE_PROPERTY,
"https://github.com/mozilla/geckodriver",
"https://github.com/mozilla/geckodriver/releases");
}
@Override
protected ImmutableList<String> createArgs() {
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--port=%d", getPort()));
if (firefoxBinary != null) {
argsBuilder.add("-b");
argsBuilder.add(firefoxBinary.getPath());
} // else GeckoDriver will be responsible for finding Firefox on the PATH or via a capability.
return argsBuilder.build();
}
@Override
protected GeckoDriverService createDriverService(File exe, int port,
ImmutableList<String> args,
ImmutableMap<String, String> environment) {
try {
GeckoDriverService service = new GeckoDriverService(exe, port, args, environment);
String firefoxLogFile = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE);
if (firefoxLogFile != null) { // System property has higher precedence
if ("/dev/stdout".equals(firefoxLogFile)) {
service.sendOutputTo(System.out);
} else if ("/dev/stderr".equals(firefoxLogFile)) {
service.sendOutputTo(System.err);
} else if ("/dev/null".equals(firefoxLogFile)) {
service.sendOutputTo(ByteStreams.nullOutputStream());
} else {
// TODO: The stream is leaked.
service.sendOutputTo(new FileOutputStream(firefoxLogFile));
}
} else {
if (getLogFile() != null) {
// TODO: This stream is leaked.
service.sendOutputTo(new FileOutputStream(getLogFile()));
} else {
service.sendOutputTo(System.err);
}
}
return service;
} catch (IOException e) {
throw new WebDriverException(e);
}
}
}
}