23
23
import com .google .common .collect .ImmutableList ;
24
24
import com .google .common .collect .ImmutableMap ;
25
25
26
- import io . appium . java_client . remote . AndroidMobileCapabilityType ;
27
- import io . appium . java_client . remote . MobileCapabilityType ;
26
+ import com . google . gson . Gson ;
27
+ import com . google . gson . GsonBuilder ;
28
28
import io .appium .java_client .service .local .flags .ServerArgument ;
29
29
30
30
import org .apache .commons .io .IOUtils ;
31
31
import org .apache .commons .lang3 .StringUtils ;
32
32
import org .apache .commons .lang3 .SystemUtils ;
33
33
import org .apache .commons .validator .routines .InetAddressValidator ;
34
34
import org .openqa .selenium .Capabilities ;
35
- import org .openqa .selenium .Platform ;
36
35
import org .openqa .selenium .os .ExecutableFinder ;
37
36
import org .openqa .selenium .remote .BrowserType ;
38
37
import org .openqa .selenium .remote .DesiredCapabilities ;
54
53
import java .util .concurrent .TimeUnit ;
55
54
import java .util .function .Function ;
56
55
57
-
58
56
public final class AppiumServiceBuilder
59
57
extends DriverService .Builder <AppiumDriverLocalService , AppiumServiceBuilder > {
60
58
@@ -73,8 +71,6 @@ public final class AppiumServiceBuilder
73
71
private static final String NODE_PATH = "NODE_BINARY_PATH" ;
74
72
75
73
public static final String BROADCAST_IP_ADDRESS = "0.0.0.0" ;
76
- private static final List <String > PATH_CAPABILITIES = ImmutableList .of (AndroidMobileCapabilityType .KEYSTORE_PATH ,
77
- AndroidMobileCapabilityType .CHROMEDRIVER_EXECUTABLE , MobileCapabilityType .APP );
78
74
private static final Path APPIUM_PATH_SUFFIX = Paths .get ("appium" , "build" , "lib" , "main.js" );
79
75
public static final int DEFAULT_APPIUM_PORT = 4723 ;
80
76
private final Map <String , String > serverArguments = new HashMap <>();
@@ -300,79 +296,15 @@ private void loadPathToMainScript() {
300
296
this .appiumJS = findMainScript ();
301
297
}
302
298
303
- private String parseCapabilitiesIfWindows () {
304
- String result = StringUtils .EMPTY ;
305
-
306
- if (capabilities != null ) {
307
- Map <String , ?> capabilitiesMap = capabilities .asMap ();
308
- Set <? extends Map .Entry <String , ?>> entries = capabilitiesMap .entrySet ();
309
-
310
- for (Map .Entry <String , ?> entry : entries ) {
311
- Object value = entry .getValue ();
312
-
313
- if (value == null ) {
314
- continue ;
315
- }
316
-
317
- if (String .class .isAssignableFrom (value .getClass ())) {
318
- if (PATH_CAPABILITIES .contains (entry .getKey ())) {
319
- value = "\\ \" " + String .valueOf (value ).replace ("\\ " , "/" ) + "\\ \" " ;
320
- } else {
321
- value = "\\ \" " + value + "\\ \" " ;
322
- }
323
- } else {
324
- value = String .valueOf (value );
325
- }
326
-
327
- String key = "\\ \" " + entry .getKey () + "\\ \" " ;
328
- if (StringUtils .isBlank (result )) {
329
- result = key + ": " + value ;
330
- } else {
331
- result = result + ", " + key + ": " + value ;
332
- }
333
- }
334
- }
335
-
336
- return "{" + result + "}" ;
337
- }
338
-
339
- private String parseCapabilitiesIfUNIX () {
340
- String result = StringUtils .EMPTY ;
341
-
342
- if (capabilities != null ) {
343
- Map <String , ?> capabilitiesMap = capabilities .asMap ();
344
- Set <? extends Map .Entry <String , ?>> entries = capabilitiesMap .entrySet ();
345
-
346
- for (Map .Entry <String , ?> entry : entries ) {
347
- Object value = entry .getValue ();
348
-
349
- if (value == null ) {
350
- continue ;
351
- }
352
-
353
- if (String .class .isAssignableFrom (value .getClass ())) {
354
- value = "\" " + value + "\" " ;
355
- } else {
356
- value = String .valueOf (value );
357
- }
358
-
359
- String key = "\" " + entry .getKey () + "\" " ;
360
- if (StringUtils .isBlank (result )) {
361
- result = key + ": " + value ;
362
- } else {
363
- result = result + ", " + key + ": " + value ;
364
- }
365
- }
366
- }
367
-
368
- return "{" + result + "}" ;
369
- }
370
-
371
- private String parseCapabilities () {
372
- if (Platform .getCurrent ().is (Platform .WINDOWS )) {
373
- return parseCapabilitiesIfWindows ();
374
- }
375
- return parseCapabilitiesIfUNIX ();
299
+ private String capabilitiesToCmdlineArg () {
300
+ Gson gson = new GsonBuilder ()
301
+ .disableHtmlEscaping ()
302
+ .serializeNulls ()
303
+ .create ();
304
+ // Selenium internally uses org.apache.commons.exec.CommandLine
305
+ // which has the following known bug in its arguments parser:
306
+ // https://issues.apache.org/jira/browse/EXEC-54
307
+ return gson .toJson (capabilities .asMap ());
376
308
}
377
309
378
310
@ Override
@@ -418,7 +350,7 @@ protected ImmutableList<String> createArgs() {
418
350
419
351
if (capabilities != null ) {
420
352
argList .add ("--default-capabilities" );
421
- argList .add (parseCapabilities ());
353
+ argList .add (capabilitiesToCmdlineArg ());
422
354
}
423
355
424
356
return new ImmutableList .Builder <String >().addAll (argList ).build ();
0 commit comments