Skip to content

Commit 75135e2

Browse files
refactor: Update the implementation of Appium executables detection (#1256)
1 parent f5470d9 commit 75135e2

13 files changed

+230
-354
lines changed

src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.appium.java_client.service.local;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static io.appium.java_client.service.local.AppiumServiceBuilder.BROADCAST_IP_ADDRESS;
2021
import static org.slf4j.event.Level.DEBUG;
2122
import static org.slf4j.event.Level.INFO;
2223

@@ -67,8 +68,8 @@ public final class AppiumDriverLocalService extends DriverService {
6768
private CommandLine process = null;
6869

6970
AppiumDriverLocalService(String ipAddress, File nodeJSExec, int nodeJSPort,
70-
ImmutableList<String> nodeJSArgs, ImmutableMap<String, String> nodeJSEnvironment,
71-
long startupTimeout, TimeUnit timeUnit) throws IOException {
71+
ImmutableList<String> nodeJSArgs, ImmutableMap<String, String> nodeJSEnvironment,
72+
long startupTimeout, TimeUnit timeUnit) throws IOException {
7273
super(nodeJSExec, nodeJSPort, nodeJSArgs, nodeJSEnvironment);
7374
this.nodeJSExec = nodeJSExec;
7475
this.nodeJSArgs = nodeJSArgs;
@@ -91,11 +92,13 @@ public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder
9192
*
9293
* @return The base URL for the managed appium server.
9394
*/
94-
@Override public URL getUrl() {
95+
@Override
96+
public URL getUrl() {
9597
return url;
9698
}
9799

98-
@Override public boolean isRunning() {
100+
@Override
101+
public boolean isRunning() {
99102
lock.lock();
100103
try {
101104
if (process == null) {
@@ -121,15 +124,15 @@ public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder
121124
}
122125

123126
private void ping(long time, TimeUnit timeUnit) throws UrlChecker.TimeoutException, MalformedURLException {
124-
URL status = new URL(url.toString() + "/status");
127+
// The operating system might block direct access to the universal broadcast IP address
128+
URL status = new URL(url.toString().replace(BROADCAST_IP_ADDRESS, "127.0.0.1") + "/status");
125129
new UrlChecker().waitUntilAvailable(time, timeUnit, status);
126130
}
127131

128132
/**
129133
* Starts the defined appium server.
130134
*
131-
* @throws AppiumServerHasNotBeenStartedLocallyException
132-
* If an error occurs while spawning the child process.
135+
* @throws AppiumServerHasNotBeenStartedLocallyException If an error occurs while spawning the child process.
133136
* @see #stop()
134137
*/
135138
public void start() throws AppiumServerHasNotBeenStartedLocallyException {
@@ -141,16 +144,16 @@ public void start() throws AppiumServerHasNotBeenStartedLocallyException {
141144

142145
try {
143146
process = new CommandLine(this.nodeJSExec.getCanonicalPath(),
144-
nodeJSArgs.toArray(new String[] {}));
147+
nodeJSArgs.toArray(new String[]{}));
145148
process.setEnvironmentVariables(nodeJSEnvironment);
146149
process.copyOutputTo(stream);
147150
process.executeAsync();
148151
ping(startupTimeout, timeUnit);
149152
} catch (Throwable e) {
150153
destroyProcess();
151154
String msgTxt = "The local appium server has not been started. "
152-
+ "The given Node.js executable: " + this.nodeJSExec.getAbsolutePath()
153-
+ " Arguments: " + nodeJSArgs.toString() + " " + "\n";
155+
+ "The given Node.js executable: " + this.nodeJSExec.getAbsolutePath()
156+
+ " Arguments: " + nodeJSArgs.toString() + " " + "\n";
154157
if (process != null) {
155158
String processStream = process.getStdOut();
156159
if (!StringUtils.isBlank(processStream)) {
@@ -171,7 +174,8 @@ public void start() throws AppiumServerHasNotBeenStartedLocallyException {
171174
*
172175
* @see #start()
173176
*/
174-
@Override public void stop() {
177+
@Override
178+
public void stop() {
175179
lock.lock();
176180
try {
177181
if (process != null) {
@@ -192,8 +196,7 @@ private void destroyProcess() {
192196
/**
193197
* Logs as string.
194198
*
195-
* @return String logs if the server has been run.
196-
* null is returned otherwise.
199+
* @return String logs if the server has been run. Null is returned otherwise.
197200
*/
198201
@Nullable
199202
public String getStdOut() {
@@ -206,6 +209,7 @@ public String getStdOut() {
206209

207210
/**
208211
* Adds other output stream which should accept server output data.
212+
*
209213
* @param outputStream is an instance of {@link OutputStream}
210214
* that is ready to accept server output
211215
*/
@@ -216,6 +220,7 @@ public void addOutPutStream(OutputStream outputStream) {
216220

217221
/**
218222
* Adds other output streams which should accept server output data.
223+
*
219224
* @param outputStreams is a list of additional {@link OutputStream}
220225
* that are ready to accept server output
221226
*/
@@ -240,12 +245,12 @@ public boolean clearOutPutStreams() {
240245
* <a href="http://slf4j.org">SLF4J</a> loggers. This allow server output
241246
* data to be configured with your preferred logging frameworks (e.g.
242247
* java.util.logging, logback, log4j).
243-
*
248+
*
244249
* <p>NOTE1: You might want to call method {@link #clearOutPutStreams()} before
245250
* calling this method.<br>
246251
* NOTE2: it is required that {@code --log-timestamp} server flag is
247252
* {@code false}.
248-
*
253+
*
249254
* <p>By default log messages are:
250255
* <ul>
251256
* <li>logged at {@code INFO} level, unless log message is pre-fixed by
@@ -262,7 +267,7 @@ public boolean clearOutPutStreams() {
262267
* is logged by logger {@code appium.service.xcuitest} at level
263268
* {@code DEBUG}.
264269
* <br>
265-
*
270+
*
266271
* @see #addSlf4jLogMessageConsumer(BiConsumer)
267272
*/
268273
public void enableDefaultSlf4jLoggingOfOutputData() {
@@ -280,14 +285,14 @@ public void enableDefaultSlf4jLoggingOfOutputData() {
280285
* message is parsed for its slf4j context (logger name, logger level etc.)
281286
* and the specified {@code BiConsumer} is invoked with the log message and
282287
* slf4j context.
283-
*
288+
*
284289
* <p>Use this method only if you want a behavior that differentiates from the
285290
* default behavior as enabled by method
286291
* {@link #enableDefaultSlf4jLoggingOfOutputData()}.
287-
*
292+
*
288293
* <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before
289294
* calling this method.
290-
*
295+
*
291296
* <p>implementation detail:
292297
* <ul>
293298
* <li>if log message begins with {@code [debug]} then log level is set to
@@ -302,10 +307,9 @@ public void enableDefaultSlf4jLoggingOfOutputData() {
302307
* Example log-message: "[debug] [XCUITest] Xcode version set to 'x.y.z' "
303308
* is logged by {@code appium.service.xcuitest} at level {@code DEBUG}
304309
* <br>
305-
*
306-
* @param slf4jLogMessageConsumer
307-
* BiConsumer block to be executed when a log message is
308-
* available.
310+
*
311+
* @param slf4jLogMessageConsumer BiConsumer block to be executed when a log message is
312+
* available.
309313
*/
310314
public void addSlf4jLogMessageConsumer(BiConsumer<String, Slf4jLogMessageContext> slf4jLogMessageConsumer) {
311315
checkNotNull(slf4jLogMessageConsumer, "slf4jLogMessageConsumer parameter is NULL!");
@@ -331,17 +335,15 @@ static Slf4jLogMessageContext parseSlf4jContextFromLogMessage(String logMessage)
331335
/**
332336
* When a complete log message is available (from server output data), the
333337
* specified {@code Consumer} is invoked with that log message.
334-
*
338+
*
335339
* <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before
336340
* calling this method.
337-
*
341+
*
338342
* <p>If the Consumer fails and throws an exception the exception is logged (at
339343
* WARN level) and execution continues.
340344
* <br>
341-
*
342-
* @param consumer
343-
* Consumer block to be executed when a log message is available.
344-
*
345+
*
346+
* @param consumer Consumer block to be executed when a log message is available.
345347
*/
346348
public void addLogMessageConsumer(Consumer<String> consumer) {
347349
checkNotNull(consumer, "consumer parameter is NULL!");

0 commit comments

Comments
 (0)