package utility;
import java.io.File;

import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

public class AppiumServerStartAndStopService {
    static String Appium_Node_Path=Constant.PATH_TO_NODE_DIRECTORY;
    static String Appium_JS_Path=Constant.PATH_TO_APPIUM_JAVASCRIPT;
    static AppiumDriverLocalService service1;
    static AppiumDriverLocalService service2;
    static String service_url1;
    static String service_url2;

    public static void appiumServer1Start() throws Exception{
        service1 = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
                usingPort(Integer.parseInt(Constant.APPIUM_COMMON_PORT)).usingDriverExecutable(new File(Appium_Node_Path)).
                withAppiumJS(new File(Appium_JS_Path)));
        service1.start();
        service_url1 = service1.getUrl().toString();
        System.out.println("URL:" +service_url1);
    }
    public static void appiumServer2Start() throws Exception{
        service2 = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
        		withArgument(() -> "--webdriveragent-port", Constant.WEBDRIVERAGENT_PORT).
        		withIPAddress(Constant.SERVER2_ADDRESS).
                usingDriverExecutable(new File(Appium_Node_Path)).
                withAppiumJS(new File(Appium_JS_Path)));
        service2.start();
        service_url2 = service2.getUrl().toString();
        System.out.println("URL:" +service_url2);
    }
    
	public static void startAppiumServer1IfNecessary() throws Exception{
		if(null==service1){
			System.out.println("Appium Server 1 is not running, let's start it.");
			appiumServer1Start();
		}else if(!service1.isRunning()){
			System.out.println("Restarting Appium Server 1.");
			appiumServer1Start();
		}		
	}
	
	public static void startAppiumServer2IfNecessary() throws Exception{
		if(null==service2){
			System.out.println("Appium Server 2 is not running, let's start it.");
			appiumServer2Start();
		}else if(!service2.isRunning()){
			System.out.println("Restarting Appium Server 2.");
			appiumServer2Start();
		}		
	}

    public static void appiumServer1Stop() throws Exception{
        service1.stop();
    }
    public static void appiumServer2Stop() throws Exception{
        service2.stop();
    }
}

@@ -30,16 +30,105 @@
 io.File;
+%0Aimport java.io.OutputStream;%0Aimport java.lang.reflect.Field;%0Aimport java.util.ArrayList;
 %0A%0Aimport
@@ -595,16 +595,142 @@
 ce_url2;
+%0A    static Field streamField1;%0A    static Field streamFields1;%0A    static Field streamField2;%0A    static Field streamFields2;
 %0A%0A    pu
@@ -1188,545 +1188,2043 @@
     
-%7D%0A    public static void appiumServer2Start() throws Exception%7B%0A        service2 = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().%0A        %09%09withArgument(() -%3E %22--webdriveragent-port%22, Constant.WEBDRIVERAGENT_PORT).%0A        %09%09withIPAddress(Constant.SERVER2_ADDRESS).%0A                usingDriverExecutable(new File(Appium_Node_Path)).%0A                withAppiumJS(new File(Appium_JS_Path)));%0A        service2.start();%0A        service_url2 = service2.getUrl().toString();%0A        System.out.println(%22URL:%22 +service_url2);
+    %0A        //workaround for server logs displayed in the console%0A        try %7B%0A            streamField1 = AppiumDriverLocalService.class.getDeclaredField(%22stream%22);%0A            streamField1.setAccessible(true);%0A            streamFields1 = Class.forName(%22io.appium.java_client.service.local.ListOutputStream%22)%0A                    .getDeclaredField(%22streams%22);%0A            streamFields1.setAccessible(true);%0A        %7D catch (ClassNotFoundException %7C NoSuchFieldException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        try %7B%0A            ((ArrayList%3COutputStream%3E) streamFields1.get(streamField1.get(service1))).clear(); // remove System.out logging%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A    public static void appiumServer2Start() throws Exception%7B%0A        service2 = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().%0A        %09%09withArgument(() -%3E %22--webdriveragent-port%22, Constant.WEBDRIVERAGENT_PORT).%0A        %09%09withIPAddress(Constant.SERVER2_ADDRESS).%0A                usingDriverExecutable(new File(Appium_Node_Path)).%0A                withAppiumJS(new File(Appium_JS_Path)));%0A        service2.start();%0A        service_url2 = service2.getUrl().toString();%0A        System.out.println(%22URL:%22 +service_url2);%0A        %0A        //workaround for server logs displayed in the console%0A        try %7B%0A            streamField2 = AppiumDriverLocalService.class.getDeclaredField(%22stream%22);%0A            streamField2.setAccessible(true);%0A            streamFields2 = Class.forName(%22io.appium.java_client.service.local.ListOutputStream%22)%0A                    .getDeclaredField(%22streams%22);%0A            streamFields2.setAccessible(true);%0A        %7D catch (ClassNotFoundException %7C NoSuchFieldException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        try %7B%0A            ((ArrayList%3COutputStream%3E) streamFields2.get(streamField2.get(service2))).clear(); // remove System.out logging%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A   

Stop displaying appium servers logs in the console.