From 4bde1247f63b508ee2163de4c4e7f67917bf9204 Mon Sep 17 00:00:00 2001 From: sudhanvam Date: Tue, 3 Jul 2018 19:31:01 +0530 Subject: [PATCH 1/3] Support for User spcified Server arguments --- pom.xml | 2 +- .../com/hariharanweb/helpers/Helpers.java | 78 +++++++++++++++++++ .../com/hariharanweb/helpers/ServerArgs.java | 37 +++++++++ .../remoteappiummanager/Server.java | 2 +- .../controller/AppiumController.java | 50 ++++++++++-- 5 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/hariharanweb/helpers/ServerArgs.java diff --git a/pom.xml b/pom.xml index 1dd74de..beacbeb 100644 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,7 @@ io.appium java-client - 6.0.0-BETA2 + 6.0.0 com.sparkjava diff --git a/src/main/java/com/hariharanweb/helpers/Helpers.java b/src/main/java/com/hariharanweb/helpers/Helpers.java index 907739d..069f0cd 100644 --- a/src/main/java/com/hariharanweb/helpers/Helpers.java +++ b/src/main/java/com/hariharanweb/helpers/Helpers.java @@ -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; @@ -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; + } + } } diff --git a/src/main/java/com/hariharanweb/helpers/ServerArgs.java b/src/main/java/com/hariharanweb/helpers/ServerArgs.java new file mode 100644 index 0000000..9ce0f55 --- /dev/null +++ b/src/main/java/com/hariharanweb/helpers/ServerArgs.java @@ -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; + } +} diff --git a/src/main/java/com/hariharanweb/remoteappiummanager/Server.java b/src/main/java/com/hariharanweb/remoteappiummanager/Server.java index 00c0186..2f8eb4e 100644 --- a/src/main/java/com/hariharanweb/remoteappiummanager/Server.java +++ b/src/main/java/com/hariharanweb/remoteappiummanager/Server.java @@ -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); diff --git a/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java b/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java index 5162db3..86d6e1a 100644 --- a/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java +++ b/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java @@ -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; @@ -10,21 +12,34 @@ 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; + JsonElement 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]; + request.queryParamsValues("SERVER_CAPS"); + + JsonObject obj = helper.parseJson(request.body()); + + JsonElement serverPath = obj.get("APPIUM_PATH"); + JsonElement userPort = obj.get("PORT"); + serverCaps = obj.get("SERVER_CAPS"); + + 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(); @@ -71,6 +86,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( @@ -82,5 +102,23 @@ private AppiumDriverLocalService startAppiumServer(String path, String port) thr return appiumDriverLocalService; } + private void addUserServerCaps (AppiumServiceBuilder builder){ + + Set> entries = serverCaps.getAsJsonObject().entrySet(); + + if(!entries.isEmpty()) { + + for (Map.Entry 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); + } + } + } + } } From a15ff23160a6e21dd549171b68b35cd37972254c Mon Sep 17 00:00:00 2001 From: sudhanvam Date: Tue, 3 Jul 2018 19:50:23 +0530 Subject: [PATCH 2/3] Removed unwanted code --- .../remoteappiummanager/controller/AppiumController.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java b/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java index 86d6e1a..1b78f94 100644 --- a/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java +++ b/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java @@ -24,8 +24,6 @@ public class AppiumController { String appiumPath = null; String port = null; - request.queryParamsValues("SERVER_CAPS"); - JsonObject obj = helper.parseJson(request.body()); JsonElement serverPath = obj.get("APPIUM_PATH"); From e4f8c898dae0751ae8ce4629f0e8452639c35e75 Mon Sep 17 00:00:00 2001 From: sudhanvam Date: Wed, 4 Jul 2018 12:24:32 +0530 Subject: [PATCH 3/3] Changes server caps data type to string from Json Element --- .../remoteappiummanager/controller/AppiumController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java b/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java index 1b78f94..93d8597 100644 --- a/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java +++ b/src/main/java/com/hariharanweb/remoteappiummanager/controller/AppiumController.java @@ -17,7 +17,7 @@ public class AppiumController { AppiumDriverLocalService appiumDriverLocalService; - JsonElement serverCaps; + String serverCaps; Helpers helper = new Helpers(); public Route startAppium = (request, response) -> { @@ -28,7 +28,7 @@ public class AppiumController { JsonElement serverPath = obj.get("APPIUM_PATH"); JsonElement userPort = obj.get("PORT"); - serverCaps = obj.get("SERVER_CAPS"); + serverCaps = obj.get("SERVER_CAPS").getAsString(); if (!serverPath.equals(JsonNull.INSTANCE)) { appiumPath = serverPath.getAsString(); @@ -102,7 +102,7 @@ private AppiumDriverLocalService startAppiumServer(String path, String port) thr private void addUserServerCaps (AppiumServiceBuilder builder){ - Set> entries = serverCaps.getAsJsonObject().entrySet(); + Set> entries = helper.parseJson(serverCaps).getAsJsonObject().entrySet(); if(!entries.isEmpty()) {