17
17
package io .appium .java_client .service .local ;
18
18
19
19
import static com .google .common .base .Preconditions .checkNotNull ;
20
+ import static io .appium .java_client .service .local .AppiumServiceBuilder .BROADCAST_IP_ADDRESS ;
20
21
import static org .slf4j .event .Level .DEBUG ;
21
22
import static org .slf4j .event .Level .INFO ;
22
23
@@ -67,8 +68,8 @@ public final class AppiumDriverLocalService extends DriverService {
67
68
private CommandLine process = null ;
68
69
69
70
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 {
72
73
super (nodeJSExec , nodeJSPort , nodeJSArgs , nodeJSEnvironment );
73
74
this .nodeJSExec = nodeJSExec ;
74
75
this .nodeJSArgs = nodeJSArgs ;
@@ -91,11 +92,13 @@ public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder
91
92
*
92
93
* @return The base URL for the managed appium server.
93
94
*/
94
- @ Override public URL getUrl () {
95
+ @ Override
96
+ public URL getUrl () {
95
97
return url ;
96
98
}
97
99
98
- @ Override public boolean isRunning () {
100
+ @ Override
101
+ public boolean isRunning () {
99
102
lock .lock ();
100
103
try {
101
104
if (process == null ) {
@@ -121,15 +124,15 @@ public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder
121
124
}
122
125
123
126
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" );
125
129
new UrlChecker ().waitUntilAvailable (time , timeUnit , status );
126
130
}
127
131
128
132
/**
129
133
* Starts the defined appium server.
130
134
*
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.
133
136
* @see #stop()
134
137
*/
135
138
public void start () throws AppiumServerHasNotBeenStartedLocallyException {
@@ -141,16 +144,16 @@ public void start() throws AppiumServerHasNotBeenStartedLocallyException {
141
144
142
145
try {
143
146
process = new CommandLine (this .nodeJSExec .getCanonicalPath (),
144
- nodeJSArgs .toArray (new String [] {}));
147
+ nodeJSArgs .toArray (new String []{}));
145
148
process .setEnvironmentVariables (nodeJSEnvironment );
146
149
process .copyOutputTo (stream );
147
150
process .executeAsync ();
148
151
ping (startupTimeout , timeUnit );
149
152
} catch (Throwable e ) {
150
153
destroyProcess ();
151
154
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 " ;
154
157
if (process != null ) {
155
158
String processStream = process .getStdOut ();
156
159
if (!StringUtils .isBlank (processStream )) {
@@ -171,7 +174,8 @@ public void start() throws AppiumServerHasNotBeenStartedLocallyException {
171
174
*
172
175
* @see #start()
173
176
*/
174
- @ Override public void stop () {
177
+ @ Override
178
+ public void stop () {
175
179
lock .lock ();
176
180
try {
177
181
if (process != null ) {
@@ -192,8 +196,7 @@ private void destroyProcess() {
192
196
/**
193
197
* Logs as string.
194
198
*
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.
197
200
*/
198
201
@ Nullable
199
202
public String getStdOut () {
@@ -206,6 +209,7 @@ public String getStdOut() {
206
209
207
210
/**
208
211
* Adds other output stream which should accept server output data.
212
+ *
209
213
* @param outputStream is an instance of {@link OutputStream}
210
214
* that is ready to accept server output
211
215
*/
@@ -216,6 +220,7 @@ public void addOutPutStream(OutputStream outputStream) {
216
220
217
221
/**
218
222
* Adds other output streams which should accept server output data.
223
+ *
219
224
* @param outputStreams is a list of additional {@link OutputStream}
220
225
* that are ready to accept server output
221
226
*/
@@ -240,12 +245,12 @@ public boolean clearOutPutStreams() {
240
245
* <a href="http://slf4j.org">SLF4J</a> loggers. This allow server output
241
246
* data to be configured with your preferred logging frameworks (e.g.
242
247
* java.util.logging, logback, log4j).
243
- *
248
+ *
244
249
* <p>NOTE1: You might want to call method {@link #clearOutPutStreams()} before
245
250
* calling this method.<br>
246
251
* NOTE2: it is required that {@code --log-timestamp} server flag is
247
252
* {@code false}.
248
- *
253
+ *
249
254
* <p>By default log messages are:
250
255
* <ul>
251
256
* <li>logged at {@code INFO} level, unless log message is pre-fixed by
@@ -262,7 +267,7 @@ public boolean clearOutPutStreams() {
262
267
* is logged by logger {@code appium.service.xcuitest} at level
263
268
* {@code DEBUG}.
264
269
* <br>
265
- *
270
+ *
266
271
* @see #addSlf4jLogMessageConsumer(BiConsumer)
267
272
*/
268
273
public void enableDefaultSlf4jLoggingOfOutputData () {
@@ -280,14 +285,14 @@ public void enableDefaultSlf4jLoggingOfOutputData() {
280
285
* message is parsed for its slf4j context (logger name, logger level etc.)
281
286
* and the specified {@code BiConsumer} is invoked with the log message and
282
287
* slf4j context.
283
- *
288
+ *
284
289
* <p>Use this method only if you want a behavior that differentiates from the
285
290
* default behavior as enabled by method
286
291
* {@link #enableDefaultSlf4jLoggingOfOutputData()}.
287
- *
292
+ *
288
293
* <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before
289
294
* calling this method.
290
- *
295
+ *
291
296
* <p>implementation detail:
292
297
* <ul>
293
298
* <li>if log message begins with {@code [debug]} then log level is set to
@@ -302,10 +307,9 @@ public void enableDefaultSlf4jLoggingOfOutputData() {
302
307
* Example log-message: "[debug] [XCUITest] Xcode version set to 'x.y.z' "
303
308
* is logged by {@code appium.service.xcuitest} at level {@code DEBUG}
304
309
* <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.
309
313
*/
310
314
public void addSlf4jLogMessageConsumer (BiConsumer <String , Slf4jLogMessageContext > slf4jLogMessageConsumer ) {
311
315
checkNotNull (slf4jLogMessageConsumer , "slf4jLogMessageConsumer parameter is NULL!" );
@@ -331,17 +335,15 @@ static Slf4jLogMessageContext parseSlf4jContextFromLogMessage(String logMessage)
331
335
/**
332
336
* When a complete log message is available (from server output data), the
333
337
* specified {@code Consumer} is invoked with that log message.
334
- *
338
+ *
335
339
* <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before
336
340
* calling this method.
337
- *
341
+ *
338
342
* <p>If the Consumer fails and throws an exception the exception is logged (at
339
343
* WARN level) and execution continues.
340
344
* <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.
345
347
*/
346
348
public void addLogMessageConsumer (Consumer <String > consumer ) {
347
349
checkNotNull (consumer , "consumer parameter is NULL!" );
0 commit comments