@@ -46,12 +46,17 @@ public final class AppiumServiceBuilder extends DriverService.Builder<AppiumDriv
46
46
47
47
private static final int DEFAULT_APPIUM_PORT = 4723 ;
48
48
49
- private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM = "npm -g ls --depth=0" ;
50
- private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN = "npm.cmd -g ls --depth=0" ;
49
+ private static final String NODE_COMMAND_PREFIX = defineNodeCommandPrefix ();
50
+
51
+ private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM = NODE_COMMAND_PREFIX +
52
+ " npm -g ls --depth=0" ;
53
+ private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN = NODE_COMMAND_PREFIX +
54
+ " npm.cmd -g ls --depth=0" ;
51
55
52
56
private static final int REQUIRED_MAJOR_NODE_JS = 0 ;
53
57
private static final int REQUIRED_MINOR_NODE_JS = 0 ;
54
58
59
+
55
60
final Map <String , String > serverArguments = new HashMap <>();
56
61
private File appiumJS ;
57
62
private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS ;
@@ -79,13 +84,19 @@ private static String getProcessOutput(InputStream stream ) throws IOException {
79
84
private static void validateNodeJSVersion (){
80
85
Runtime rt = Runtime .getRuntime ();
81
86
String result = null ;
87
+ Process p = null ;
82
88
try {
83
- Process p = rt .exec (" node -v" );
89
+ p = rt .exec (NODE_COMMAND_PREFIX + " node -v" );
84
90
p .waitFor ();
85
91
result = getProcessOutput (p .getInputStream ());
86
92
} catch (Exception e ) {
87
93
throw new InvalidNodeJSInstance ("Node.js is not installed" , e );
88
94
}
95
+ finally {
96
+ if (p != null )
97
+ p .destroy ();
98
+ }
99
+
89
100
String versionNum = result .replace ("v" ,"" );
90
101
String [] tokens = versionNum .split ("\\ ." );
91
102
if (Integer .parseInt (tokens [0 ]) < REQUIRED_MAJOR_NODE_JS ||
@@ -97,13 +108,18 @@ private static void validateNodeJSVersion(){
97
108
private static File findNodeInCurrentFileSystem (){
98
109
Runtime rt = Runtime .getRuntime ();
99
110
String instancePath ;
111
+ Process p = null ;
100
112
try {
101
- Process p = rt .exec (returnCommandThatSearchesForDefaultNode ());
113
+ p = rt .exec (returnCommandThatSearchesForDefaultNode ());
102
114
p .waitFor ();
103
115
instancePath = getProcessOutput (p .getInputStream ());
104
116
} catch (Exception e ) {
105
117
throw new RuntimeException (e );
106
118
}
119
+ finally {
120
+ if (p != null )
121
+ p .destroy ();
122
+ }
107
123
108
124
File result ;
109
125
if (StringUtils .isBlank (instancePath ) || !(result = new File (instancePath + File .separator + NODE_MODULES_FOLDER +
@@ -128,6 +144,13 @@ private static void validateNodeStructure(File node){
128
144
absoluteNodePath + "doesn't match " + APPIUM_NODE_MASK );
129
145
}
130
146
147
+ private static String defineNodeCommandPrefix () {
148
+ if (Platform .getCurrent ().is (Platform .WINDOWS ))
149
+ return "cmd.exe /C" ;
150
+ else
151
+ return "/bin/bash -l -c" ;
152
+ }
153
+
131
154
public AppiumServiceBuilder () {
132
155
usingPort (DEFAULT_APPIUM_PORT );
133
156
}
@@ -138,21 +161,25 @@ protected File findDefaultExecutable() {
138
161
Runtime rt = Runtime .getRuntime ();
139
162
Process p ;
140
163
try {
141
- p = rt .exec (" node" );
164
+ p = rt .exec (NODE_COMMAND_PREFIX + " node" );
142
165
} catch (IOException e ) {
143
166
throw new RuntimeException (e );
144
167
}
145
- OutputStream outputStream = p .getOutputStream ();
146
- PrintStream out = new PrintStream (outputStream ) ;
147
- out .println ("console.log(process.execPath);" ) ;
148
- out .close ();
149
168
150
169
try {
170
+ OutputStream outputStream = p .getOutputStream ();
171
+ PrintStream out = new PrintStream (outputStream ) ;
172
+ out .println ("console.log(process.execPath);" ) ;
173
+ out .close ();
174
+
151
175
return new File (getProcessOutput (p .getInputStream ()));
152
176
}
153
177
catch (Throwable t ){
154
178
throw new RuntimeException (t );
155
179
}
180
+ finally {
181
+ p .destroy ();
182
+ }
156
183
}
157
184
158
185
/**
0 commit comments