Launching and stopping appium server programmtically

I am using code at 10.10.1 with java 1.7

on windows with windows format command its working. But on mac its doing nothing not even error. Now I am stuck. I appreciate if someone guide me, what I am doing wrong?

Should be :

CommandLine command = new CommandLine(“/bin/sh”);

As soon as I removed -c, this exception started coming
/Applications/Appium.app/Contents/Resources/node/bin/node: /Applications/Appium.app/Contents/Resources/node/bin/node: cannot execute binary file

1 Like

I left the hope with command line. Now I am using process builder and its working ok.

List list = new ArrayList();
list.add("/Applications/Appium.app/Contents/Resources/node/bin/node");
list.add("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
list.add("–address");
list.add(“127.0.0.1”);
list.add("–port");
list.add(“4723”);
list.add("–no-reset");
// create the process builder
ProcessBuilder pb = new ProcessBuilder(list);
try {
pb.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

I just tried with CommandLine and it worked after I changed:

CommandLine command = new CommandLine(“/bin/sh -c”);
command.addArgument(“/Applications/Appium.app/Contents/Resources/node/bin/node”,false);

to be:

CommandLine command = new CommandLine(“/Applications/Appium.app/Contents/Resources/node/bin/node”);

I am not sure what has changed since the last time, but it’s working now without invoking the shell app.

Give it a try and let me know how it works.

@BeforeMethod
public void setUp() throws Exception {
	
	Runtime.getRuntime().exec("/bin/bash export ANDROID_HOME=$HOME/Downloads/adt-bundle-mac-x86_64/sdk/");
	
	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
	command.addArgument("--address", false);
	command.addArgument("127.0.0.1");
	command.addArgument("--port", false);
	command.addArgument("4723");
	command.addArgument("--no-reset", false);
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	DefaultExecutor executor = new DefaultExecutor();
	executor.setExitValue(1);
	executor.execute(command, resultHandler);
	
	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability("appium-version", "1.0");
	capabilities.setCapability("platformName", "iOS");
	capabilities.setCapability("platformVersion", "7.1");
	capabilities.setCapability("deviceName", "iPhone 6 Plus");
	capabilities.setCapability("app", "/Users/Prithivi/Documents/Sample.app");
	
	driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	driver.findElement(By.name("Continue")).click();

This works for me very well… :smile:

Note: I used java-client-1.3.0.jar, in latest version AppiumDriver was changed into interface.

thanks @Prithivi and @Hassan_Radi mine also worked. But now I am not able to create session. Even I used your
Runtime.getRuntime().exec("/bin/bash export ANDROID_HOME=$HOME/Downloads/adt-bundle-mac-x86_64/sdk/");
to set the path then also, its showing me error as 31merrore[39m: Failed to start an Appium session, err was: Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
e[36minfoe[39m: [debug] Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at null. (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-adb/lib/adb.js:113:12)
at ChildProcess.exithandler (child_process.js:652:7)
at ChildProcess.emit (events.js:98:17)
at maybeClose (child_process.js:756:16)
at Socket. (child_process.js:969:11)
at Socket.emit (events.js:95:17)
at Pipe.close (net.js:465:12)
e[36minfoe[39m: [debug] Responding to client with error: {“status”:33,“value”:{“message”:“A new session could not be created. (Original error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.)”,“origValue”:“Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.”},“sessionId”:null}
e[36minfoe[39m: e[37m<-- POST /wd/hub/session e[39me[31m500e[39me[90m 7040.310 ms - 342e[39m e[90me[39m
Exception while getting driver instance

1 Like

Are you sure the path you given is correct ? It looks exactly like mine.

Just try passing these values also,

PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/build-tools
PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/platform-tools
PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/tools

not working same issue

Add ANDROID_HOME and tools path in “.bash_profile” file.

both is there, echo also shows the path

Prithivi,

For me also same issue …Not able to start session …Can u please help me…

Please find the error log below

Error log:
[36minfoe[39m: Welcome to Appium v1.3.1 (REV 1160ce02bb89c354cb99317985123acf39f0e7d3)
e[36minfoe[39m: Appium REST http interface listener started on 127.0.0.1:4723
e[36minfoe[39m: [debug] Non-default server args: {“address”:“127.0.0.1”,“noReset”:true}
Client/4.3.4 (java 1.5)
e[36minfoe[39m: [debug] Using local app from desired caps: /Users/jagatheswaran/Documents/AppiumPOC/android.apk
e[36minfoe[39m: [debug] Creating new appium session 5084c368-8b59-4005-92c6-8ee6a1c95116
e[36minfoe[39m: Starting android appium
e[36minfoe[39m: [debug] Using fast reset? false
e[36minfoe[39m: [debug] Preparing device for session
e[36minfoe[39m: [debug] Checking whether app is actually present
e[36minfoe[39m: [debug] Checking whether adb is present
e[33mwarne[39m: The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb.
e[36minfoe[39m: [debug] Sent shutdown command, waiting for UiAutomator to stop…
e[33mwarne[39m: UiAutomator did not shut down fast enough, calling it gone
e[31merrore[39m: Failed to start an Appium session, err was: Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
e[36minfoe[39m: [debug] Cleaning up android objects
e[36minfoe[39m: [debug] Cleaning up appium session
e[36minfoe[39m: [debug] Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at null. (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-adb/lib/adb.js:113:12)
at ChildProcess.exithandler (child_process.js:652:7)
at ChildProcess.emit (events.js:98:17)
at maybeClose (child_process.js:756:16)
at Socket. (child_process.js:969:11)
at Socket.emit (events.js:95:17)
at Pipe.close (net.js:465:12)
e[36minfoe[39m: [debug] Responding to client with error: {“status”:33,“value”:{“message”:“A new session could not be created. (Original error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.)”,“origValue”:“Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.”},“sessionId”:null}
e[36minfoe[39m: e[37m<-- POST /wd/hub/session e[39me[31m500e[39me[90m 7020.937 ms - 342e[39m e[90me[39m
Exception in thread “main” org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 9.28 seconds
Build info: version: ‘2.44.0’, revision: ‘76d78cf’, time: ‘2014-10-23 20:02:37’
System info: host: ‘administrators-MacBook-Pro-9.local’, ip: ‘10.0.0.3’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.9.5’, java.version: ‘1.7.0_60’
Driver info: io.appium.java_client.AppiumDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:95)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:49)
at asd.main(asd.java:41)

“The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb.”

This error will occurs when ANDRIOD_HOME is set, but it was wrong. Check your path variables and try to add “/” in the end like,
“PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/build-tools”
into
“PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/build-tools/”

I was facing the same problem. This post helped a lot to resolve it.
But I made a mistake of including the bash EXPORT command after launching Appium server. For anyone out there still facing the issue, take care of the order

Hi Anuradha,

Can u please share your steps and code…I am still facing the same problem.

Thanks,
Jagatheswaran.S

Hi Jagatheskmp,

I had the same issue when i start the Appium server from Eclipse Java code. I was new to MAC and eventually came to know the environment variables set using EXPORT command and the ~/.bash_profile or ~/.profile will be available only for TERMINAL application.

For the environment variable to be available for Eclipse or any other application other than TERMINAL, please refer [link][1]
This fixed my problem. Hope this helps you too! Feel free to contact me incase you have any difficulties.
[1]: http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-mac-os-x-launchd-plist/

Hi Praveen,

Still i am getting the same problem,Can u please guide me and can u please send some more stps to do…

Thanks,
Jagathes

Hi Jagatheswaran,

Please share the steps you have followed in setting up the Appium from the start to till now. Lets start from there.
Also, include version of Mac, Appium etc.

Steps to set up environment variables depend on the Mac OS version. I assume you have followed the link exactly.

http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-mac-os-x-launchd-plist/

Regards
Praveen MS

Hi,

I Launch the Appium app using the following code but not able to launch the Appium server(i.e Launch button in Appium). I would like to know how to Launch the Appium server through java code.

Runtime.getRuntime().exec("open /Applications/Appium.app");	

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("deviceName", "android");
capabilities.setCapability("app", "chrome");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("http://www.google.com");
System.out.println("Android Title is: " + driver.getTitle());
driver.quit();

Please me to come of this Struggle.