Triggering Appium from the command line via Java code


#1

I have done the appium start up using Command line arguments in Java code.
When I run the code in eclipse 7 out of 10 times it starts the appium server. But remaining time it fails to start up.

The reason I found is after execute command the server is not starting fastly. Since the appium is not started the code goes in to the application process and when there is no appium connection it gets fail.

CODE:

CommandLine command = new CommandLine(“cmd”);
command.addArgument("/c");
command.addArgument(“D:\programfiles\node.exe”);
command.addArgument(“D:\programfiles\node_modules\appium\bin\appium.js”);
command.addArgument("–address", true);
command.addArgument(“127.0.0.1”);
command.addArgument("–port", false);
command.addArgument(“4725”);
command.addArgument("–full-reset", true);

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);


#2

It’s not optimal, but you could put a Thread.sleep(int milliseconds) right after your command execution to give the server enough time to start.

A better answer may be found in this topic : Launching and stopping appium server programmtically


#3

@Alexis Thank you very much for your reply. In my current code itself I have used
Thread.sleep(30000); [30seconds]
But still the issue is exist.


#4

Any suggestions @Hassan_Radi.


#5

Please give this a try and let me know how it works with you: https://github.com/Genium-Framework/Appium-Support/wiki


#7

I tried with the Appium support library:

CODE:

File file = new File(“D:/programfiles”);

ServerArguments serverArguments = new ServerArguments();

serverArguments.setArgument("–address",“127.0.0.1”);
serverArguments.setArgument("–chromedriver-port", 9516);
serverArguments.setArgument("–bootstrap-port", 4725);
serverArguments.setArgument("–no-reset", true);
serverArguments.setArgument("–local-timezone", true);

AppiumServer appiumServer = new AppiumServer(file, serverArguments);
appiumServer.startServer();

System.out.println(“started”);

CONSOLE:
Aug 12, 2015 10:41:35 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server is starting…

After that nothing is happening

Appium Installation Directory
node.exe= D:\programfiles\node.exe
appium.js= D:\programfiles\node_modules\appium\bin\appium.js


#8

this line should cause a compile time error as there is no such constructor.

Second issue is that the constructor doesn’t take a File object, it takes a String representation of the path.

Please change the whole line with the following line, give it a try and let me know how it works:

AppiumServer appiumServer = new AppiumServer(serverArguments, “D:/programfiles”);


#9

I have tried that but it gives me an error like

With the file object

Passing as a String


#10

Please check my updated post above


#11

I have tried with the string representation also and updated the screenshot in the previous post. PFA


#12

This is the constructor you are trying to invoke: https://github.com/Genium-Framework/Appium-Support/blob/master/src/main/java/com/github/genium_framework/appium/support/server/AppiumServer.java#L112


#13

With reference to the Constructor only I passed like

AppiumServer appiumServer = new AppiumServer(file, serverArguments);

But when changing the code line like

AppiumServer appiumServer = new AppiumServer(serverArguments,file);
OR
AppiumServer appiumServer = new AppiumServer(serverArguments,“D:/programfiles”);

It says it is undefined.

Do I miss some thing here?


#14

Sorry for the confusion, it was a documentation issue and it is fixed now.


#15

This is the correct usage of the constructor. Is it giving you any errors??


#16

It is not giving any errors. Once the appiumServer.startServer(); is initiated.

It is not displaying any information in the console.Apart from this below Console log

Aug 12, 2015 11:13:27 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server is starting…


#17

ok… if it failed to start the server within 30 seconds it should throw a timeout exception. Please let it run for that amount of time and show me the exception details.


#18

I run the code and waited for 5 minutes. Till now I didn’t receive any exceptions in the console log.

In the code after the appiumServer.startServer(); method I have added the System.out.println(“Server started”);

Even it is not moving to the next line.


#19

Now, I purposely made some wrong changes in the code like

File file = new File(“D:/programfiles”);

ServerArguments serverArguments = new ServerArguments();

serverArguments.setArgument(“address”,“127.0.0.1”); [removed the ‘–’]
serverArguments.setArgument("–chromedriver-port", 9516);
serverArguments.setArgument("–bootstrap-port", 4725);
serverArguments.setArgument("–no-reset", true);
serverArguments.setArgument("–local-timezone", true);

AppiumServer appiumServer = new AppiumServer(file, serverArguments);
appiumServer.startServer();

System.out.println(“started”)

When I run the above code I get an exception like:

SUCCESS: The process “node.exe” with PID 5824 has been terminated.
Appium server is starting
Aug 12, 2015 11:28:56 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server is starting…
Aug 12, 2015 11:28:56 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:28:57 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:28:58 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:28:59 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:01 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:02 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:03 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:04 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:05 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:06 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:07 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:08 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:09 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:11 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:12 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:13 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:14 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:15 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:16 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:17 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:18 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:19 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:20 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:22 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:23 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:24 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:25 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Exception in thread “main” com.github.genium_framework.server.exception.ServerTimeoutException: The server didn’t start after 30 seconds with error:
usage: appium.js [-h] [-v] [–shell]
[–localizable-strings-dir LOCALIZABLESTRINGSDIR] [–app APP]
[–ipa IPA] [-U UDID] [-a ADDRESS] [-p PORT]
[-ca CALLBACKADDRESS] [-cp CALLBACKPORT] [-bp BOOTSTRAPPORT]
[-k] [-r BACKENDRETRIES] [–session-override] [–full-reset]
[–no-reset] [-l] [-lt LAUNCHTIMEOUT] [-g LOG]
[–log-level {info,info:debug,info:info,info:warn,info:error,warn,warn:debug,warn:info,warn:warn,warn:error,error,error:debug,error:info,error:warn,error:error,debug,debug:debug,debug:info,debug:warn,debug:error}]
[–log-timestamp] [–local-timezone] [–log-no-colors]
[-G WEBHOOK] [–native-instruments-lib]
[–app-pkg ANDROIDPACKAGE] [–app-activity ANDROIDACTIVITY]
[–app-wait-package ANDROIDWAITPACKAGE]
[–app-wait-activity ANDROIDWAITACTIVITY]
[–android-coverage ANDROIDCOVERAGE] [–avd AVD]
[–avd-args AVDARGS]
[–device-ready-timeout ANDROIDDEVICEREADYTIMEOUT] [–safari]
[–device-name DEVICENAME] [–platform-name PLATFORMNAME]
[–platform-version PLATFORMVERSION]
[–automation-name AUTOMATIONNAME]
[–browser-name BROWSERNAME] [–default-device]
[–force-iphone] [–force-ipad] [–language LANGUAGE]
[–locale LOCALE] [–calendar-format CALENDARFORMAT]
[–orientation ORIENTATION]
[–tracetemplate AUTOMATIONTRACETEMPLATEPATH]
[–instruments INSTRUMENTSPATH] [–show-sim-log]
[–show-ios-log] [–nodeconfig NODECONFIG] [-ra ROBOTADDRESS]
[-rp ROBOTPORT] [–selendroid-port SELENDROIDPORT]
[–chromedriver-port CHROMEDRIVERPORT]
[–chromedriver-executable CHROMEDRIVEREXECUTABLE]
[–use-keystore] [–keystore-path KEYSTOREPATH]
[–keystore-password KEYSTOREPASSWORD] [–key-alias KEYALIAS]
[–key-password KEYPASSWORD] [–show-config]
[–no-perms-check] [–command-timeout DEFAULTCOMMANDTIMEOUT]
[–keep-keychains] [–strict-caps] [–isolate-sim-device]
[–tmp TMPDIR] [–trace-dir TRACEDIR]
[–intent-action INTENTACTION]
[–intent-category INTENTCATEGORY]
[–intent-flags INTENTFLAGS]
[–intent-args OPTIONALINTENTARGUMENTS]
[–dont-stop-app-on-reset] [–debug-log-spacing]
[–suppress-adb-kill-server] [–async-trace]

appium.js: error: Unrecognized arguments: address 127.0.0.1.

The error was correctly thrown at the console.

Now I added the ‘–’ infront of the address and I run the code.

No exceptions thrown in the screen.

In the File Object do we need to pass node.exe path or appium.js path
Because in command line arguments I will pass both of the directory path.


#20

no, you don’t need to pass any of these paths. can you please check if you have a process running that is called “node.exe” after running the same code.

Also please can you provide your OS details, Appium server version and JDK version.


#21

Yes when running the code the node.exe process is started. PFA

Java version
java version “1.7.0_51”
Java™ SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot™ 64-Bit Server VM (build 24.51-b03, mixed mode)

OS: Windows 7 64 bit

Appium Server: 1.4.0.0