Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Adding server capabilities support for ATD #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.0.0-BETA2</version>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/com/hariharanweb/helpers/Helpers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.hariharanweb.helpers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.appium.java_client.service.local.flags.GeneralServerFlag;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -77,4 +82,77 @@ public int getPid(Process process) {
return -1;
}
}

public JsonObject parseJson( String jsonLine) {
JsonElement jelement = new JsonParser().parse(jsonLine);
return jelement.getAsJsonObject();
}

public GeneralServerFlag getServerArgument ( String capability ) {

switch (capability) {
case "SESSION_OVERRIDE":
return ServerArgs.SESSION_OVERRIDE.getArgument();

case "PRE_LAUNCH":
return ServerArgs.PRE_LAUNCH.getArgument();

case "LOG_LEVEL":
return ServerArgs.LOG_LEVEL.getArgument();

case "RELAXED_SECURITY":
return ServerArgs.RELAXED_SECURITY.getArgument();

case "CALLBACK_ADDRESS":
return ServerArgs.CALLBACK_ADDRESS.getArgument();

case "CALLBACK_PORT":
return ServerArgs.CALLBACK_PORT.getArgument();

case "LOG_TIMESTAMP":
return ServerArgs.LOG_TIMESTAMP.getArgument();

case "LOCAL_TIMEZONE":
return ServerArgs.LOCAL_TIMEZONE.getArgument();

case "LOG_NO_COLORS":
return ServerArgs.LOG_NO_COLORS.getArgument();

case "WEB_HOOK":
return ServerArgs.WEB_HOOK.getArgument();

case "CONFIGURATION_FILE":
return ServerArgs.CONFIGURATION_FILE.getArgument();

case "ROBOT_ADDRESS":
return ServerArgs.ROBOT_ADDRESS.getArgument();

case "ROBOT_PORT":
return ServerArgs.ROBOT_PORT.getArgument();

case "SHOW_CONFIG":
return ServerArgs.SHOW_CONFIG.getArgument();

case "NO_PERMS_CHECKS":
return ServerArgs.NO_PERMS_CHECKS.getArgument();

case "STRICT_CAPS":
return ServerArgs.STRICT_CAPS.getArgument();

case "TEMP_DIRECTORY":
return ServerArgs.TEMP_DIRECTORY.getArgument();

case "DEBUG_LOG_SPACING":
return ServerArgs.DEBUG_LOG_SPACING.getArgument();

case "ASYNC_TRACE":
return ServerArgs.ASYNC_TRACE.getArgument();

case "ENABLE_HEAP_DUMP":
return ServerArgs.ENABLE_HEAP_DUMP.getArgument();

default:
return null;
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/hariharanweb/helpers/ServerArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.hariharanweb.helpers;

import io.appium.java_client.service.local.flags.GeneralServerFlag;

public enum ServerArgs {

SESSION_OVERRIDE(GeneralServerFlag.SESSION_OVERRIDE),
PRE_LAUNCH(GeneralServerFlag.PRE_LAUNCH),
LOG_LEVEL(GeneralServerFlag.LOG_LEVEL),
RELAXED_SECURITY(GeneralServerFlag.RELAXED_SECURITY),
CALLBACK_ADDRESS(GeneralServerFlag.CALLBACK_ADDRESS),
CALLBACK_PORT(GeneralServerFlag.CALLBACK_PORT),
LOG_TIMESTAMP(GeneralServerFlag.LOG_TIMESTAMP),
LOCAL_TIMEZONE(GeneralServerFlag.LOCAL_TIMEZONE),
LOG_NO_COLORS(GeneralServerFlag.LOG_NO_COLORS),
WEB_HOOK(GeneralServerFlag.WEB_HOOK),
CONFIGURATION_FILE(GeneralServerFlag.CONFIGURATION_FILE),
ROBOT_ADDRESS(GeneralServerFlag.ROBOT_ADDRESS),
ROBOT_PORT(GeneralServerFlag.ROBOT_PORT),
SHOW_CONFIG(GeneralServerFlag.SHOW_CONFIG),
NO_PERMS_CHECKS(GeneralServerFlag.NO_PERMS_CHECKS),
STRICT_CAPS(GeneralServerFlag.STRICT_CAPS),
TEMP_DIRECTORY(GeneralServerFlag.TEMP_DIRECTORY),
DEBUG_LOG_SPACING(GeneralServerFlag.DEBUG_LOG_SPACING),
ASYNC_TRACE(GeneralServerFlag.ASYNC_TRACE),
ENABLE_HEAP_DUMP(GeneralServerFlag.ENABLE_HEAP_DUMP);

private GeneralServerFlag serverFlag;

ServerArgs ( GeneralServerFlag args) {
this.serverFlag = args;
}

public GeneralServerFlag getArgument() {
return this.serverFlag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void main(String[] args) throws IOException {
get("/stop/:udid", deviceController.stopADBLog, new JsonTransformer());
});
path("/appium", () -> {
get("/start", appiumController.startAppium, new JsonTransformer());
post("/start", appiumController.startAppium, new JsonTransformer());
get("/stop", appiumController.stopAppium, new JsonTransformer());
get("/isRunning", appiumController.isAppiumServerRunning, new JsonTransformer());
get("/logs", appiumController.getAppiumLogs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hariharanweb.remoteappiummanager.controller;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.hariharanweb.helpers.Helpers;
import io.appium.java_client.service.local.AppiumDriverLocalService;
Expand All @@ -10,21 +12,32 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

public class AppiumController {
AppiumDriverLocalService appiumDriverLocalService;
String serverCaps;
Helpers helper = new Helpers();

public Route startAppium = (request, response) -> {
String appiumPath = null;
String port = null;

String[] urlParameter = request.queryParamsValues("URL");
String[] userPort = request.queryParamsValues("PORT");
if (urlParameter != null && urlParameter[0] != null) {
appiumPath = urlParameter[0];
JsonObject obj = helper.parseJson(request.body());

JsonElement serverPath = obj.get("APPIUM_PATH");
JsonElement userPort = obj.get("PORT");
serverCaps = obj.get("SERVER_CAPS").getAsString();

if (!serverPath.equals(JsonNull.INSTANCE)) {
appiumPath = serverPath.getAsString();
}
if (userPort != null && userPort[0] != null) {
port = userPort[0];

if (!userPort.equals(JsonNull.INSTANCE) ) {
port = userPort.getAsString();
}

startAppiumServer(appiumPath,port);
URL url = appiumDriverLocalService.getUrl();
JsonObject jsonObject = new JsonObject();
Expand Down Expand Up @@ -71,6 +84,11 @@ private AppiumDriverLocalService startAppiumServer(String path, String port) thr
} else {
builder.usingAnyFreePort();
}

if(serverCaps != null) {
addUserServerCaps(builder);
}

appiumDriverLocalService = AppiumDriverLocalService.buildService(builder);
appiumDriverLocalService.start();
System.out.println(
Expand All @@ -82,5 +100,23 @@ private AppiumDriverLocalService startAppiumServer(String path, String port) thr
return appiumDriverLocalService;
}

private void addUserServerCaps (AppiumServiceBuilder builder){

Set<Map.Entry<String, JsonElement>> entries = helper.parseJson(serverCaps).getAsJsonObject().entrySet();

if(!entries.isEmpty()) {

for (Map.Entry<String, JsonElement> entry : entries) {

GeneralServerFlag serverArgument = helper.getServerArgument(entry.getKey());

if(serverArgument != null && !entry.getValue().toString().equals("null")){
builder.withArgument(serverArgument, entry.getValue().getAsString());
}
else if(serverArgument !=null && entry.getValue().toString().equals("null")){
builder.withArgument(serverArgument);
}
}
}
}
}