forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppiumServiceBuilder.java
484 lines (431 loc) · 17.7 KB
/
AppiumServiceBuilder.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* 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.collect.ImmutableList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.appium.java_client.internal.ReflectionHelpers;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileBrowserType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.flags.GeneralServerFlag;
import io.appium.java_client.service.local.flags.ServerArgument;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.validator.routines.InetAddressValidator;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.os.ExecutableFinder;
import org.openqa.selenium.remote.Browser;
import org.openqa.selenium.remote.service.DriverService;
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
public final class AppiumServiceBuilder
extends DriverService.Builder<AppiumDriverLocalService, AppiumServiceBuilder> {
/**
* The environmental variable used to define
* the path to executable appium.js (1.4.x and lower) or
* main.js (1.5.x and higher).
*/
public static final String APPIUM_PATH = "APPIUM_BINARY_PATH";
/**
* The environmental variable used to define
* the path to executable NodeJS file (node.exe for WIN and
* node for Linux/MacOS X).
*/
private static final String NODE_PATH = "NODE_BINARY_PATH";
public static final String BROADCAST_IP_ADDRESS = "0.0.0.0";
private static final Path APPIUM_PATH_SUFFIX = Paths.get("appium", "build", "lib", "main.js");
public static final int DEFAULT_APPIUM_PORT = 4723;
private final Map<String, String> serverArguments = new HashMap<>();
private File appiumJS;
private File node;
private String ipAddress = BROADCAST_IP_ADDRESS;
private Capabilities capabilities;
private boolean autoQuoteCapabilitiesOnWindows = false;
private static final Function<File, String> APPIUM_JS_NOT_EXIST_ERROR = (fullPath) -> String.format(
"The main Appium script does not exist at '%s'", fullPath.getAbsolutePath());
private static final Function<File, String> NODE_JS_NOT_EXIST_ERROR = (fullPath) ->
String.format("The main NodeJS executable does not exist at '%s'", fullPath.getAbsolutePath());
private static final List<String> PATH_CAPABILITIES = ImmutableList.of(AndroidMobileCapabilityType.KEYSTORE_PATH,
AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE, MobileCapabilityType.APP);
public AppiumServiceBuilder() {
usingPort(DEFAULT_APPIUM_PORT);
withEnvironment(System.getenv());
}
/**
* Provides a measure of how strongly this {@link DriverService} supports the given
* {@code capabilities}. A score of 0 or less indicates that this {@link DriverService} does not
* support instances of {@link org.openqa.selenium.WebDriver} that require {@code capabilities}.
* Typically, the score is generated by summing the number of capabilities that the driver
* service directly supports that are unique to the driver service (that is, things like
* "{@code proxy}" don't tend to count to the score).
* Higher the score, higher the possibility of getting grid sessions created sooner.
*/
@Override
public int score(Capabilities capabilities) {
int score = 0;
if (capabilities.getCapability(PLATFORM_NAME) != null) {
score++;
}
String browserName = capabilities.getBrowserName();
if (Browser.CHROME.is(browserName) || browserName.equalsIgnoreCase(MobileBrowserType.ANDROID)
|| Browser.SAFARI.is(browserName)) {
score++;
}
return score;
}
private static File validatePath(@Nullable String fullPath, String errMsg) {
if (fullPath == null) {
throw new InvalidServerInstanceException(errMsg);
}
File result = new File(fullPath);
if (!result.exists()) {
throw new InvalidServerInstanceException(errMsg);
}
return result;
}
private static File findBinary(String name, String errMsg) {
return validatePath(new ExecutableFinder().find(name), errMsg);
}
private static File findNpm() {
return findBinary("npm",
"Node Package Manager (npm) is either not installed or its executable is not present in PATH");
}
private static File findMainScript() {
File npm = findNpm();
List<String> cmdLine = SystemUtils.IS_OS_WINDOWS
// npm is a batch script, so on windows we need to use cmd.exe in order to execute it
? Arrays.asList("cmd.exe", "/c", String.format("\"%s\" root -g", npm.getAbsolutePath()))
: Arrays.asList(npm.getAbsolutePath(), "root", "-g");
ProcessBuilder pb = new ProcessBuilder(cmdLine);
String nodeModulesRoot;
try {
nodeModulesRoot = IOUtils.toString(pb.start().getInputStream(), StandardCharsets.UTF_8).trim();
} catch (IOException e) {
throw new InvalidServerInstanceException(
"Cannot retrieve the path to the folder where NodeJS modules are located", e);
}
File mainAppiumJs = Paths.get(nodeModulesRoot, APPIUM_PATH_SUFFIX.toString()).toFile();
if (!mainAppiumJs.exists()) {
throw new InvalidServerInstanceException(APPIUM_JS_NOT_EXIST_ERROR.apply(mainAppiumJs));
}
return mainAppiumJs;
}
protected File findDefaultExecutable() {
if (this.node != null) {
validatePath(this.node.getAbsolutePath(), NODE_JS_NOT_EXIST_ERROR.apply(this.node));
return this.node;
}
File node = loadPathFromEnv(NODE_PATH);
if (node != null) {
validatePath(node.getAbsolutePath(), NODE_JS_NOT_EXIST_ERROR.apply(node));
this.node = node;
return this.node;
}
this.node = findBinary("node",
"NodeJS is either not installed or its executable not present in PATH");
return this.node;
}
/**
* Boolean arguments have a special moment:
* the presence of an arguments means "true". This method
* was designed for these cases.
*
* @param argument is an instance which contains the argument name.
* @return the self-reference.
*/
public AppiumServiceBuilder withArgument(ServerArgument argument) {
serverArguments.put(argument.getArgument(), "");
return this;
}
/**
* Adds a server argument.
*
* @param argument is an instance which contains the argument name.
* @param value A non null string value. (Warn!!!) Boolean arguments have a special moment:
* the presence of an arguments means "true". At this case an empty string
* should be defined.
* @return the self-reference.
*/
public AppiumServiceBuilder withArgument(ServerArgument argument, String value) {
String argName = argument.getArgument();
switch (argName) {
case "--port":
case "-p":
usingPort(Integer.parseInt(value));
break;
case "--address":
case "-a":
withIPAddress(value);
break;
case "--log":
case "-g":
withLogFile(new File(value));
break;
case "--base-path":
serverArguments.put(argName, sanitizeBasePath(value));
break;
default:
serverArguments.put(argName, value);
break;
}
return this;
}
private static String sanitizeBasePath(String basePath) {
basePath = checkNotNull(basePath).trim();
checkArgument(
!basePath.isEmpty(),
"Given base path is not valid - blank or empty values are not allowed for base path"
);
return basePath.endsWith("/") ? basePath : basePath + "/";
}
/**
* Adds capabilities.
*
* @param capabilities is an instance of {@link Capabilities}.
* @return the self-reference.
*/
public AppiumServiceBuilder withCapabilities(Capabilities capabilities) {
this.capabilities = (this.capabilities == null ? capabilities : this.capabilities)
.merge(capabilities);
return this;
}
/**
* Adds capabilities.
*
* @param capabilities is an instance of {@link Capabilities}.
* @param autoQuoteCapabilitiesOnWindows automatically escape quote all
* capabilities when calling appium.
* This is required on windows systems only.
* @return the self-reference.
*/
public AppiumServiceBuilder withCapabilities(Capabilities capabilities,
boolean autoQuoteCapabilitiesOnWindows) {
this.autoQuoteCapabilitiesOnWindows = autoQuoteCapabilitiesOnWindows;
return withCapabilities(capabilities);
}
/**
* Sets an executable appium.js.
*
* @param appiumJS an executable appium.js (1.4.x and lower) or
* main.js (1.5.x and higher).
* @return the self-reference.
*/
public AppiumServiceBuilder withAppiumJS(File appiumJS) {
this.appiumJS = appiumJS;
return this;
}
public AppiumServiceBuilder withIPAddress(String ipAddress) {
this.ipAddress = ipAddress;
return this;
}
@Nullable
private static File loadPathFromEnv(String envVarName) {
String fullPath = System.getProperty(envVarName);
if (StringUtils.isBlank(fullPath)) {
fullPath = System.getenv(envVarName);
}
return StringUtils.isBlank(fullPath) ? null : new File(fullPath);
}
private void loadPathToMainScript() {
if (this.appiumJS != null) {
validatePath(this.appiumJS.getAbsolutePath(), APPIUM_JS_NOT_EXIST_ERROR.apply(this.appiumJS));
return;
}
File mainScript = loadPathFromEnv(APPIUM_PATH);
if (mainScript != null) {
validatePath(mainScript.getAbsolutePath(), APPIUM_JS_NOT_EXIST_ERROR.apply(mainScript));
this.appiumJS = mainScript;
return;
}
this.appiumJS = findMainScript();
}
private String capabilitiesToQuotedCmdlineArg() {
if (capabilities == null) {
return "{}";
}
StringBuilder result = new StringBuilder();
Map<String, ?> capabilitiesMap = capabilities.asMap();
Set<? extends Map.Entry<String, ?>> entries = capabilitiesMap.entrySet();
for (Map.Entry<String, ?> entry : entries) {
Object value = entry.getValue();
if (value == null) {
continue;
}
if (value instanceof String) {
String valueString = (String) value;
if (PATH_CAPABILITIES.contains(entry.getKey())) {
value = "\\\"" + valueString.replace("\\", "/") + "\\\"";
} else {
value = "\\\"" + valueString + "\\\"";
}
} else {
value = String.valueOf(value);
}
String key = "\\\"" + entry.getKey() + "\\\"";
if (result.length() > 0) {
result.append(", ");
}
result.append(key).append(": ").append(value);
}
return "{" + result + "}";
}
private String capabilitiesToCmdlineArg() {
if (autoQuoteCapabilitiesOnWindows && Platform.getCurrent().is(Platform.WINDOWS)) {
return capabilitiesToQuotedCmdlineArg();
}
Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.serializeNulls()
.create();
// Selenium internally uses org.apache.commons.exec.CommandLine
// which has the following known bug in its arguments parser:
// https://issues.apache.org/jira/browse/EXEC-54
return gson.toJson(capabilities.asMap());
}
@Override
protected ImmutableList<String> createArgs() {
List<String> argList = new ArrayList<>();
loadPathToMainScript();
argList.add(appiumJS.getAbsolutePath());
argList.add("--port");
argList.add(String.valueOf(getPort()));
if (StringUtils.isBlank(ipAddress)) {
ipAddress = BROADCAST_IP_ADDRESS;
} else {
InetAddressValidator validator = InetAddressValidator.getInstance();
if (!validator.isValid(ipAddress) && !validator.isValidInet4Address(ipAddress)
&& !validator.isValidInet6Address(ipAddress)) {
throw new IllegalArgumentException(
"The invalid IP address " + ipAddress + " is defined");
}
}
argList.add("--address");
argList.add(ipAddress);
File log = getLogFile();
if (log != null) {
argList.add("--log");
argList.add(log.getAbsolutePath());
}
Set<Map.Entry<String, String>> entries = serverArguments.entrySet();
for (Map.Entry<String, String> entry : entries) {
String argument = entry.getKey();
String value = entry.getValue();
if (StringUtils.isBlank(argument) || value == null) {
continue;
}
argList.add(argument);
if (!StringUtils.isBlank(value)) {
argList.add(value);
}
}
if (capabilities != null) {
argList.add("--default-capabilities");
argList.add(capabilitiesToCmdlineArg());
}
return new ImmutableList.Builder<String>().addAll(argList).build();
}
@Override
public AppiumDriverLocalService build() {
File driverExecutable = ReflectionHelpers.getPrivateFieldValue(
DriverService.Builder.class, this, "exe", File.class
);
if (driverExecutable == null) {
usingDriverExecutable(findDefaultExecutable());
}
return super.build();
}
/**
* Sets which Node.js the builder will use.
*
* @param nodeJSExecutable The executable Node.js to use.
* @return A self reference.
*/
@Override
public AppiumServiceBuilder usingDriverExecutable(File nodeJSExecutable) {
return super.usingDriverExecutable(nodeJSExecutable);
}
/**
* Sets which port the appium server should be started on. A value of 0 indicates that any
* free port may be used.
*
* @param port The port to use; must be non-negative.
* @return A self reference.
*/
@Override
public AppiumServiceBuilder usingPort(int port) {
return super.usingPort(port);
}
/**
* Configures the appium server to start on any available port.
*
* @return A self reference.
*/
@Override
public AppiumServiceBuilder usingAnyFreePort() {
return super.usingAnyFreePort();
}
/**
* Defines the environment for the launched appium server.
*
* @param environment A map of the environment variables to launch the
* appium server with.
* @return A self reference.
*/
@Override
public AppiumServiceBuilder withEnvironment(Map<String, String> environment) {
return super.withEnvironment(environment);
}
/**
* Configures the appium server to write log to the given file.
*
* @param logFile A file to write log to.
* @return A self reference.
*/
@Override
public AppiumServiceBuilder withLogFile(File logFile) {
return super.withLogFile(logFile);
}
@SneakyThrows
@Override
protected AppiumDriverLocalService createDriverService(File nodeJSExecutable, int nodeJSPort,
Duration startupTimeout,
List<String> nodeArguments,
Map<String, String> nodeEnvironment) {
String basePath = serverArguments.getOrDefault(
GeneralServerFlag.BASEPATH.getArgument(), serverArguments.get("-pa"));
return new AppiumDriverLocalService(ipAddress, nodeJSExecutable, nodeJSPort, startupTimeout, nodeArguments,
nodeEnvironment).withBasePath(basePath);
}
}