How to stop and start Appium server programmatically using Java?


How to stop and start Appium server programmatically using Java?



How can I perform launching and stopping my server using Java code? Currently I am doing this process manually.





Refer my blog link - happyautomation.blogspot.in/2015/01/…
– Shekhar Swami
Jan 13 '16 at 5:04





i reffered ur blog it is showing errors ---- Starting appium server ---- ---- Appium server started Successfully ! ---- 'C:/Program' is not recognized as an internal or external command, operable program or batch file. ---- Stoping appium server ---- ---- Appium server stopped Successfully ! ---- ERROR: The process "node.exe" not found.
– sameer joshi
Jan 13 '16 at 7:19





Keep your Appium folder D or E drive or C drive.
– Shekhar Swami
Jan 13 '16 at 9:40





@Shekhar Swami installed on c drive only
– sameer joshi
Jan 14 '16 at 5:25





If you install appium at C:Program Files (x86) or C:Program Files , then java code will throw an error.Cut paste your Appium set up directory at root folder so path will be C:Appium... or D:Appium.. If you do this 'C:/Program' is not recognized as an internal or external command, error won't come. If you are still facing issue then please post your code and error
– Shekhar Swami
Jan 14 '16 at 6:28




3 Answers
3



For anybody reading this who happens to be using npm (node/js/typescript), I've created a module called appium-controller that starts and stops appium in the background programmatically (mac or windows).



Please read this - it will explain how to start https://github.com/appium/java-client/pull/240



The latest java client has api to do this.



here is one way of starting programetically, the page lists more such ways.


AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
final DesiredCapabilities capabilities = new DesiredCapabilities();
// capabilities.setCapability(CapabilityType.BROWSER_NAME, "Browser");
capabilities.setCapability("deviceName", "MiPad");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("automationName","Appium");
// capabilities.setCapability("avd","firstavd");
capabilities.setCapability("appPackage", "com");
capabilities.setCapability("appActivity", ".ui.activity");
driver = new AndroidDriver<MobileElement>(service, capabilities);





when i use the above lines, i got following Throw-able exception "org/apache/commons/validator/routines/InetAddressValidator " could you please suggest what i am missing here?
– CodeFlash
Jan 14 '16 at 12:47






mvnrepository.com/artifact/commons-validator/commons-validator/… - download the jar and add it .. it has a dependancy on this jar
– Shamik
Jan 14 '16 at 13:05



There are 3 ways to achieve the scenario.
1)Using AppiumDriverLocalService


public void startServer() {
//Set Capabilities
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");

//Build the Appium service
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL,"error");

//Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
service.start();
}

public void stopServer() {
service.stop();
}




2)Using Appium.js with Node.exe


public void startServer() {
CommandLine cmd = new CommandLine("C:\Program Files (x86)\Appium\node.exe");
cmd.addArgument("C:\Program Files (x86)\Appium\node_modules\appium\bin\Appium.js");
cmd.addArgument("--address");
cmd.addArgument("127.0.0.1");
cmd.addArgument("--port");
cmd.addArgument("4723");

DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(cmd, handler);
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}

public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("taskkill /F /IM node.exe");
} catch (IOException e) {
e.printStackTrace();
}
}




3)Start Appium server using Command Prompt


public void startServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("cmd.exe /c start cmd.exe /k "appium -a 127.0.0.1 -p 4723 --session-override -dc "{""noReset"": ""false""}""");
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}

public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("taskkill /F /IM node.exe");
runtime.exec("taskkill /F /IM cmd.exe");
} catch (IOException e) {
e.printStackTrace();
}
}<br/>



I found it helpful.Hope it helps.
Source: http://www.automationtestinghub.com/3-ways-to-start-appium-server-from-java/






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API