Launching and stopping appium server programmtically

And Console Output (Error) for This code is:

Starting Server…
Server Started…
Stopping Server
Server Stopped
ERROR: The process “node.exe” not found.

This line should not be added as it is already covered by previous points. Please remove it and see what happens.

The server didn’t start and that is why it is failing with the mentioned error. Can you try it with Java Process and see if it produces the same error??

even though if we put false for no-reset/full-reset…the logs are showing as true. Has anyone faced the same issue?

      command.addArgument("--full-reset", false);

or
command.addArgument("–no-reset", false);

The appium is launched as
e[36minfoe[39m: [debug] Non-default server args: {“address”:“127.0.0.1”,“noReset”:true,“log”:“D:/appiumLogs.txt”}

Hello Hasan,
I’ve done with the gig…
But Using Batch File. where I have put command line and trying to execute batch file.

Now same Thing I want to Do to launch Appium Server for iOS Application on Mac OS.

Please help me out as I am new to Mac…and Commands into Mac OS.

Thanks,
Ajit JAdhav.

Thanks @Hassan_Radi…For providing this code.Helped alot.
Great Job :slight_smile:

For anyone who is still having problems getting the server to start programmatically, you can refer to this post about a new library called Appium Support.

Hope it helps.

I’m getting an error like

C:/Program’ is not recognized as an internal or external command,
operable program or batch file.

CODE:

public void startAppiumServer() throws IOException, InterruptedException {   

        CommandLine command = new CommandLine("cmd");
        command.addArgument("/c");
        command.addArgument("C:/Program Files (x86)/Appium/node.exe");  
        command.addArgument("C:/Program Files (x86)/Appium/node_modules/appium/lib/appium.js");  
        command.addArgument("--address", false);  
        command.addArgument("127.0.0.1");  
        command.addArgument("--port", false);  
        command.addArgument("4723");  
        command.addArgument("--full-reset", false);  

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

Found the solution for this problem

Actually the space between the folder names is the root cause. The spaces should not occur in between the words.

C:/Program Files (x86)/Appium/node.exe

The Spaces should not occur

In Java I think you need to escape the spaces in double quotes. You could use single quotes to get around this.

1 Like

@Hassan_Radi Could you explain the meaning of it

command.addArgument("–address", true);
command.addArgument(“127.0.0.1”);

command.addArgument("–port", false);
command.addArgument(“4722”);

command.addArgument("–full-reset", false);

Hi Radha,

Can you please let us know how do you set up the ANDROID_HOME globally in MAC.

I am getting the same issue when running the appium launch via java code in eclipse

Thank you in Advance.

Regards
Gan

Hi

The same code is not working for me on Mac. Can you help me with it.

I am getting NPE at appiumProcess.getWatchdog().destroyProcess();

Please help ASAP

Could you paste all code example here and the error

Hi

Its working here after setting the capabilities properly.

Can you tell me how to launch an emulator programatically on Mac and Win.

Thanks,
Pradeep

@Pradeep simulators and emulators are both launched automatically by appium if you don’t have one already running. You can look at the appium source code for how this is done :smile:

@Hassan_Radi

public void launchAppium() {
CommandLine command = new CommandLine("/bin/sh");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
command.addArgument("–address", false);
command.addArgument(“127.0.0.1”);
command.addArgument("–port", false);
command.addArgument(“4723”);
command.addArgument("–full-reset", false);

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();  
    DefaultExecutor executor = new DefaultExecutor();  
    executor.setExitValue(1);  
    try {
		executor.execute(command, resultHandler);
	} catch (ExecuteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

When you start the server through the Appium GUI, the logs show what command Appium executes to start the sever.

You can execute the same command into terminal to get the server working. An example of starting the server from terminal is to execute the following:

cd /Applications/Appium.app/Contents/Resources/node_modules/appium

‘/Applications/Appium.app/Contents/Resources/node/bin/node’ lib/server/main.js --address “127.0.0.1” --command-timeout “7200” --session-override --debug-log-spacing --platform-version “8.4” --platform-name “iOS” --app “<path-to-.app>” --show-ios-log --show-ios-log --device-name “iPad Air” --instruments “/Applications/Xcode.app/Contents/Developer/usr/bin/instruments” --native-instruments-lib --orientation “Landscape”

Hi
I am able to invoke appium from source but when i run my test for hybrid app on an emulator for android it fires an error saying:
error: android.util.AndroidException: INSTRUMENTATION_FAILED: com.hp.eSupplies.s
elendroid/io.selendroid.server.ServerInstrumentation

    at com.android.commands.am.Am.runInstrument(Am.java:865)

    at com.android.commands.am.Am.onRun(Am.java:282)

    at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)

    at com.android.commands.am.Am.main(Am.java:76)

    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)

    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)

    at dalvik.system.NativeStart.main(Native Method)

info: [debug] Cleaning up appium session
error: Failed to start an Appium session, err was: Error: android.util.AndroidEx
ception: INSTRUMENTATION_FAILED: com.hp.eSupplies.selendroid/io.selendroid.serve
r.ServerInstrumentation

info: [debug] Error: android.util.AndroidException: INSTRUMENTATION_FAILED: com.
hp.eSupplies.selendroid/io.selendroid.server.ServerInstrumentation

at C:\Users\birmanga\node_modules\appium\node_modules\appium-adb\lib\adb.js:

1394:17
at [object Object]. (C:\Users\birmanga\node_modules\appium\node_m
odules\appium-adb\lib\adb.js:180:9)
at ChildProcess.exithandler (child_process.js:742:7)
at ChildProcess.emit (events.js:110:17)
at maybeClose (child_process.js:1015:16)
at Process.ChildProcess._handle.onexit (child_process.js:1087:5)
info: [debug] Responding to client with error: {“status”:33,“value”:{“message”:"
A new session could not be created. (Original error: android.util.AndroidExcepti
on: INSTRUMENTATION_FAILED: com.hp.eSupplies.selendroid/io.selendroid.server.Ser
verInstrumentation\r\r)",“origValue”:“android.util.AndroidException: INSTRUMENTA
TION_FAILED: com.hp.eSupplies.selendroid/io.selendroid.server.ServerInstrumentat
ion\r\r”},“sessionId”:null}
info: <-- POST /wd/hub/session 500 15246.261 ms - 380

When i try the same code by launching Appium(1.3.X) from appium.exe the code works perfectly in an emulator.

Could anyone please help me out of it. Thanks in advance.

@Hassan_Radi I am able to start the appium server below is my code and it’s output.

public class AppiumServer {

 public void startServer(){
  CommandLine command = new CommandLine("cmd");
  command.addArgument("/c");
  command.addArgument("D:/Appium/node.exe");
  command.addArgument("D:/Appium/node_modules/appium/bin/appium.js");
  command.addArgument("--address");
  command.addArgument("127.0.0.1");
  command.addArgument("--port");
  command.addArgument("4724");
  command.addArgument("--no-reset");
  command.addArgument("--log");
  command.addArgument("D:/appiumLogs.txt");
  DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
  DefaultExecutor executor = new DefaultExecutor();
  executor.setExitValue(1);
  
  
  try {
   executor.execute(command, resultHandler);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public void stopServer(){
  
  CommandLine command = new CommandLine("cmd");
  command.addArgument("/c");
  command.addArgument("taskkill");
  command.addArgument("/F");
  command.addArgument("/IM");
  command.addArgument("node.exe");
  
  DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
  DefaultExecutor executor = new DefaultExecutor();
  executor.setExitValue(1);
  
  try {
    executor.execute(command, resultHandler);
  } catch (IOException e) {
    e.printStackTrace();
  }
  
  
 }
 
 public static void main(String[] args) {
  AppiumServer server=new AppiumServer();
  System.out.println("---- Starting appium server ----");
  server.startServer();
  System.out.println("---- Appium server started Successfully ! ----");
  
  try {
    Thread.sleep(2000L);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  
  System.out.println("---- Stoping appium server ----");
  server.stopServer();
  System.out.println("---- Appium server stopped Successfully ! ----");
  
 }
 
}

Output:
---- Starting appium server ----
---- Appium server started Successfully ! ----
---- Stoping appium server ----
---- Appium server stopped Successfully ! ----
SUCCESS: The process “node.exe” with PID 6752 has been terminated.

But when i am trying to set capabilities, i am getting error as session can’t be created. Below is the code for capablities

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, “”);
capabilities.setCapability(CapabilityType.VERSION, “5.1.1”);
capabilities.setCapability(“platformName”, “ANDROID”);
capabilities.setCapability(“deviceName”, “QO4301C12715”);
capabilities.setCapability(“autoWebview”, “true”);
capabilities.setCapability(“appPackage”, “com.android.settings”);
capabilities.setCapability(“appActivity”, “com.android.calendar.LaunchActivity”);
capabilities.setCapability(“newCommandTimeout”, “180”);
capabilities.setCapability(“autoWebviewTimeout”, “6000”);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4725/wd/hub”),
capabilities);

	System.out.println("App Launched sucessfully:::");

Please help me out to resolve this issue.

Please include details regarding the error message and stack trace.