forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppiumDriverLocalService.java
454 lines (416 loc) · 17 KB
/
AppiumDriverLocalService.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.java_client.service.local;
import com.google.common.annotations.VisibleForTesting;
import io.appium.java_client.internal.ReflectionHelpers;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.net.UrlChecker;
import org.openqa.selenium.os.CommandLine;
import org.openqa.selenium.remote.service.DriverService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import javax.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.appium.java_client.service.local.AppiumServiceBuilder.BROADCAST_IP4_ADDRESS;
import static io.appium.java_client.service.local.AppiumServiceBuilder.BROADCAST_IP6_ADDRESS;
import static org.slf4j.event.Level.DEBUG;
import static org.slf4j.event.Level.INFO;
public final class AppiumDriverLocalService extends DriverService {
private static final String URL_MASK = "http://%s:%d/";
private static final Logger LOG = LoggerFactory.getLogger(AppiumDriverLocalService.class);
private static final Pattern LOGGER_CONTEXT_PATTERN = Pattern.compile("^(\\[debug\\] )?\\[(.+?)\\]");
private static final String APPIUM_SERVICE_SLF4J_LOGGER_PREFIX = "appium.service";
private static final Duration DESTROY_TIMEOUT = Duration.ofSeconds(60);
private static final Duration IS_RUNNING_PING_TIMEOUT = Duration.ofMillis(1500);
private final File nodeJSExec;
private final List<String> nodeJSArgs;
private final Map<String, String> nodeJSEnvironment;
private final Duration startupTimeout;
private final ReentrantLock lock = new ReentrantLock(true); //uses "fair" thread ordering policy
private final ListOutputStream stream = new ListOutputStream().add(System.out);
private final URL url;
private String basePath;
private CommandLine process = null;
AppiumDriverLocalService(String ipAddress, File nodeJSExec,
int nodeJSPort, Duration startupTimeout,
List<String> nodeJSArgs, Map<String, String> nodeJSEnvironment
) throws IOException {
super(nodeJSExec, nodeJSPort, startupTimeout, nodeJSArgs, nodeJSEnvironment);
this.nodeJSExec = nodeJSExec;
this.nodeJSArgs = nodeJSArgs;
this.nodeJSEnvironment = nodeJSEnvironment;
this.startupTimeout = startupTimeout;
this.url = new URL(String.format(URL_MASK, ipAddress, nodeJSPort));
}
public static AppiumDriverLocalService buildDefaultService() {
return buildService(new AppiumServiceBuilder());
}
public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder) {
return builder.build();
}
public AppiumDriverLocalService withBasePath(String basePath) {
this.basePath = basePath;
return this;
}
public String getBasePath() {
return this.basePath;
}
@SneakyThrows
private static URL addSuffix(URL url, String suffix) {
return url.toURI().resolve("." + (suffix.startsWith("/") ? suffix : "/" + suffix)).toURL();
}
@SneakyThrows
@SuppressWarnings("SameParameterValue")
private static URL replaceHost(URL source, String oldHost, String newHost) {
return new URL(source.toString().replaceFirst(oldHost, newHost));
}
/**
* Base URL.
*
* @return The base URL for the managed appium server.
*/
@Override
public URL getUrl() {
return basePath == null ? url : addSuffix(url, basePath);
}
@Override
public boolean isRunning() {
lock.lock();
try {
if (process == null || !process.isRunning()) {
return false;
}
try {
ping(IS_RUNNING_PING_TIMEOUT);
return true;
} catch (UrlChecker.TimeoutException e) {
return false;
} catch (MalformedURLException e) {
throw new AppiumServerHasNotBeenStartedLocallyException(e.getMessage(), e);
}
} finally {
lock.unlock();
}
}
private void ping(Duration timeout) throws UrlChecker.TimeoutException, MalformedURLException {
URL url = getUrl();
String host = url.getHost();
// The operating system will block direct access to the universal broadcast IP address
if (host.equals(BROADCAST_IP4_ADDRESS)) {
url = replaceHost(url, BROADCAST_IP4_ADDRESS, "127.0.0.1");
} else if (host.equals(BROADCAST_IP6_ADDRESS)) {
url = replaceHost(url, BROADCAST_IP6_ADDRESS, "::1");
}
URL status = addSuffix(url, "/status");
new UrlChecker().waitUntilAvailable(timeout.toMillis(), TimeUnit.MILLISECONDS, status);
}
/**
* Starts the defined appium server.
*
* @throws AppiumServerHasNotBeenStartedLocallyException If an error occurs while spawning the child process.
* @see #stop()
*/
public void start() throws AppiumServerHasNotBeenStartedLocallyException {
lock.lock();
try {
if (isRunning()) {
return;
}
try {
process = new CommandLine(
this.nodeJSExec.getCanonicalPath(),
nodeJSArgs.toArray(new String[]{})
);
process.setEnvironmentVariables(nodeJSEnvironment);
process.copyOutputTo(stream);
process.executeAsync();
ping(startupTimeout);
} catch (Exception e) {
final Optional<String> output = Optional.ofNullable(process)
.map(CommandLine::getStdOut)
.filter((o) -> !StringUtils.isBlank(o));
destroyProcess();
List<String> errorLines = new ArrayList<>();
errorLines.add("The local appium server has not been started");
errorLines.add(String.format("Reason: %s", e.getMessage()));
if (e instanceof UrlChecker.TimeoutException) {
errorLines.add(String.format(
"Consider increasing the server startup timeout value (currently %sms)",
startupTimeout.toMillis()
));
}
errorLines.add(
String.format("Node.js executable path: %s", nodeJSExec.getAbsolutePath())
);
errorLines.add(String.format("Arguments: %s", nodeJSArgs));
output.ifPresent((o) -> errorLines.add(String.format("Output: %s", o)));
throw new AppiumServerHasNotBeenStartedLocallyException(
StringUtils.joinWith("\n", errorLines), e
);
}
} finally {
lock.unlock();
}
}
/**
* Stops this service is it is currently running. This method will attempt to block until the
* server has been fully shutdown.
*
* @see #start()
*/
@Override
public void stop() {
lock.lock();
try {
if (process != null) {
destroyProcess();
}
process = null;
} finally {
lock.unlock();
}
}
/**
* Destroys the service if it is running.
*
* @param timeout The maximum time to wait before the process will be force-killed.
* @return The exit code of the process or zero if the process was not running.
*/
private int destroyProcess(Duration timeout) {
if (process == null || !process.isRunning()) {
return 0;
}
// This all magic is necessary, because Selenium does not publicly expose
// process killing timeouts. By default a process is killed forcibly if
// it does not exit after two seconds, which is in most cases not enough for
// Appium
try {
Object osProcess = ReflectionHelpers.getPrivateFieldValue(
process.getClass(), process, "process", Object.class
);
Object watchdog = ReflectionHelpers.getPrivateFieldValue(
osProcess.getClass(), osProcess, "executeWatchdog", Object.class
);
Process nativeProcess = ReflectionHelpers.getPrivateFieldValue(
watchdog.getClass(), watchdog, "process", Process.class
);
nativeProcess.destroy();
nativeProcess.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
LOG.warn("No explicit timeout could be applied to the process termination", e);
}
return process.destroy();
}
/**
* Destroys the service.
* This methods waits up to `DESTROY_TIMEOUT` seconds for the Appium service
* to exit gracefully.
*/
private void destroyProcess() {
destroyProcess(DESTROY_TIMEOUT);
}
/**
* Logs as string.
*
* @return String logs if the server has been run. Null is returned otherwise.
*/
@Nullable
public String getStdOut() {
if (process != null) {
return process.getStdOut();
}
return null;
}
/**
* Adds other output stream which should accept server output data.
*
* @param outputStream is an instance of {@link OutputStream}
* that is ready to accept server output
*/
public void addOutPutStream(OutputStream outputStream) {
checkNotNull(outputStream, "outputStream parameter is NULL!");
stream.add(outputStream);
}
/**
* Adds other output streams which should accept server output data.
*
* @param outputStreams is a list of additional {@link OutputStream}
* that are ready to accept server output
*/
public void addOutPutStreams(List<OutputStream> outputStreams) {
checkNotNull(outputStreams, "outputStreams parameter is NULL!");
for (OutputStream stream : outputStreams) {
addOutPutStream(stream);
}
}
/**
* Remove the outputStream which is receiving server output data.
*
* @return the outputStream has been removed if it is present
*/
public Optional<OutputStream> removeOutPutStream(OutputStream outputStream) {
checkNotNull(outputStream, "outputStream parameter is NULL!");
return stream.remove(outputStream);
}
/**
* Remove all existing server output streams.
*
* @return true if at least one output stream has been cleared
*/
public boolean clearOutPutStreams() {
return stream.clear();
}
/**
* Enables server output data logging through
* <a href="http://slf4j.org">SLF4J</a> loggers. This allow server output
* data to be configured with your preferred logging frameworks (e.g.
* java.util.logging, logback, log4j).
*
* <p>NOTE1: You might want to call method {@link #clearOutPutStreams()} before
* calling this method.<br>
* NOTE2: it is required that {@code --log-timestamp} server flag is
* {@code false}.
*
* <p>By default log messages are:
* <ul>
* <li>logged at {@code INFO} level, unless log message is pre-fixed by
* {@code [debug]} then logged at {@code DEBUG} level.</li>
* <li>logged by a <a href="http://slf4j.org">SLF4J</a> logger instance with
* a name corresponding to the appium sub module as prefixed in log message
* (logger name is transformed to lower case, no spaces and prefixed with
* "appium.service.").</li>
* </ul>
* Example log-message: "[ADB] Cannot read version codes of " is logged by
* logger: {@code appium.service.adb} at level {@code INFO}.
* <br>
* Example log-message: "[debug] [XCUITest] Xcode version set to 'x.y.z' "
* is logged by logger {@code appium.service.xcuitest} at level
* {@code DEBUG}.
* <br>
*
* @see #addSlf4jLogMessageConsumer(BiConsumer)
*/
public void enableDefaultSlf4jLoggingOfOutputData() {
addSlf4jLogMessageConsumer((logMessage, ctx) -> {
if (ctx.getLevel().equals(DEBUG)) {
ctx.getLogger().debug(logMessage);
} else {
ctx.getLogger().info(logMessage);
}
});
}
/**
* When a complete log message is available (from server output data) that
* message is parsed for its slf4j context (logger name, logger level etc.)
* and the specified {@code BiConsumer} is invoked with the log message and
* slf4j context.
*
* <p>Use this method only if you want a behavior that differentiates from the
* default behavior as enabled by method
* {@link #enableDefaultSlf4jLoggingOfOutputData()}.
*
* <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before
* calling this method.
*
* <p>implementation detail:
* <ul>
* <li>if log message begins with {@code [debug]} then log level is set to
* {@code DEBUG}, otherwise log level is {@code INFO}.</li>
* <li>the appium sub module name is parsed from the log message and used as
* logger name (prefixed with "appium.service.", all lower case, spaces
* removed). If no appium sub module is detected then "appium.service" is
* used as logger name.</li>
* </ul>
* Example log-message: "[ADB] Cannot read version codes of " is logged by
* {@code appium.service.adb} at level {@code INFO} <br>
* Example log-message: "[debug] [XCUITest] Xcode version set to 'x.y.z' "
* is logged by {@code appium.service.xcuitest} at level {@code DEBUG}
* <br>
*
* @param slf4jLogMessageConsumer BiConsumer block to be executed when a log message is
* available.
*/
public void addSlf4jLogMessageConsumer(BiConsumer<String, Slf4jLogMessageContext> slf4jLogMessageConsumer) {
checkNotNull(slf4jLogMessageConsumer, "slf4jLogMessageConsumer parameter is NULL!");
addLogMessageConsumer(logMessage -> {
slf4jLogMessageConsumer.accept(logMessage, parseSlf4jContextFromLogMessage(logMessage));
});
}
@VisibleForTesting
static Slf4jLogMessageContext parseSlf4jContextFromLogMessage(String logMessage) {
Matcher m = LOGGER_CONTEXT_PATTERN.matcher(logMessage);
String loggerName = APPIUM_SERVICE_SLF4J_LOGGER_PREFIX;
Level level = INFO;
if (m.find()) {
loggerName += "." + m.group(2).toLowerCase().replaceAll("\\s+", "");
if (m.group(1) != null) {
level = DEBUG;
}
}
return new Slf4jLogMessageContext(loggerName, level);
}
/**
* When a complete log message is available (from server output data), the
* specified {@code Consumer} is invoked with that log message.
*
* <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before
* calling this method.
*
* <p>If the Consumer fails and throws an exception the exception is logged (at
* WARN level) and execution continues.
* <br>
*
* @param consumer Consumer block to be executed when a log message is available.
*/
public void addLogMessageConsumer(Consumer<String> consumer) {
checkNotNull(consumer, "consumer parameter is NULL!");
addOutPutStream(new OutputStream() {
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@Override
public void write(int chr) {
try {
outputStream.write(chr);
if (chr == '\n') {
consumer.accept(outputStream.toString());
outputStream.reset();
}
} catch (Exception e) {
// log error and continue
LOG.warn("Log message consumer crashed!", e);
}
}
});
}
}