Skip to content

#252 fix #255

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Oct 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ public final class AppiumServiceBuilder extends DriverService.Builder<AppiumDriv

private static final int DEFAULT_APPIUM_PORT = 4723;

private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM = "npm -g ls --depth=0";
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN = "npm.cmd -g ls --depth=0";
private static final String NODE_COMMAND_PREFIX = defineNodeCommandPrefix();

private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM = NODE_COMMAND_PREFIX +
" npm -g ls --depth=0";
private final static String COMMAND_WHICH_EXTRACTS_DEFAULT_PATH_TO_APPIUM_WIN = NODE_COMMAND_PREFIX +
" npm.cmd -g ls --depth=0";

private static final int REQUIRED_MAJOR_NODE_JS = 0;
private static final int REQUIRED_MINOR_NODE_JS = 0;


final Map<String, String> serverArguments = new HashMap<>();
private File appiumJS;
private String ipAddress = DEFAULT_LOCAL_IP_ADDRESS;
Expand Down Expand Up @@ -79,13 +84,19 @@ private static String getProcessOutput(InputStream stream ) throws IOException {
private static void validateNodeJSVersion(){
Runtime rt = Runtime.getRuntime();
String result = null;
Process p = null;
try {
Process p = rt.exec("node -v");
p = rt.exec(NODE_COMMAND_PREFIX + " node -v");
p.waitFor();
result = getProcessOutput(p.getInputStream());
} catch (Exception e) {
throw new InvalidNodeJSInstance("Node.js is not installed", e);
}
finally {
if (p != null)
p.destroy();
}

String versionNum = result.replace("v","");
String[] tokens = versionNum.split("\\.");
if (Integer.parseInt(tokens[0]) < REQUIRED_MAJOR_NODE_JS ||
Expand All @@ -97,13 +108,18 @@ private static void validateNodeJSVersion(){
private static File findNodeInCurrentFileSystem(){
Runtime rt = Runtime.getRuntime();
String instancePath;
Process p = null;
try {
Process p = rt.exec(returnCommandThatSearchesForDefaultNode());
p = rt.exec(returnCommandThatSearchesForDefaultNode());
p.waitFor();
instancePath = getProcessOutput(p.getInputStream());
} catch (Exception e) {
throw new RuntimeException(e);
}
finally {
if (p != null)
p.destroy();
}

File result;
if (StringUtils.isBlank(instancePath) || !(result = new File(instancePath + File.separator + NODE_MODULES_FOLDER +
Expand All @@ -128,6 +144,13 @@ private static void validateNodeStructure(File node){
absoluteNodePath + "doesn't match " + APPIUM_NODE_MASK);
}

private static String defineNodeCommandPrefix() {
if (Platform.getCurrent().is(Platform.WINDOWS))
return "cmd.exe /C";
else
return "/bin/bash -l -c";
}

public AppiumServiceBuilder() {
usingPort(DEFAULT_APPIUM_PORT);
}
Expand All @@ -138,21 +161,25 @@ protected File findDefaultExecutable() {
Runtime rt = Runtime.getRuntime();
Process p;
try {
p = rt.exec("node");
p = rt.exec(NODE_COMMAND_PREFIX + " node");
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream outputStream = p.getOutputStream();
PrintStream out = new PrintStream(outputStream) ;
out.println("console.log(process.execPath);") ;
out.close();

try {
OutputStream outputStream = p.getOutputStream();
PrintStream out = new PrintStream(outputStream) ;
out.println("console.log(process.execPath);") ;
out.close();

return new File(getProcessOutput(p.getInputStream()));
}
catch (Throwable t){
throw new RuntimeException(t);
}
finally {
p.destroy();
}
}

/**
Expand Down