Starting Appium server programmatically on MAC

There is no appium,js file in this path /Applications/Appium.app/Contents/Resources/node_modules/appium/bin on appium.app 1.5.3 version. what would be the path in that case.
Here’s my code
public void setUp() throws ExecuteException, IOException {

    Runtime.getRuntime().exec("/bin/bash ");
    // CommandLine command = new CommandLine("/bin/sh ");
    // command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node");
    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");
    command.addArgument("127.0.0.1");
    command.addArgument("--port");
    command.addArgument("4723");
    command.addArgument("--no-reset", false);
    command.addArgument("--log");
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(1);
    executor.execute(command, resultHandler);

    String appPath = "/Users/khyati.dave/src/workspace/MobileTestAutomation/MoPubSampleApp.ipa";
    File app = new File(appPath);
    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability("platformName", "iOS");
    desiredCapabilities.setCapability("platformVersion", "9.3");
    desiredCapabilities.setCapability("deviceName", "iPhone 6");
    desiredCapabilities.setCapability("app", app.getAbsolutePath());
    driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
}

The error I get is :
Error: Cannot find module ‘/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js’
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3

If you just type appium into a console it didn’t start a session?

The path to this path has been changed has changed in newer Appium versions, the new path is:

/Applications/Appium.app/Contents/Resources/node_modules/appium/build/lib/main.js

@slipy12 from console if I do the following it does start appium ( see
screenshot ) but I want to start it from within my test . How do i do that
?
I modified my test like this :

Runtime.getRuntime().exec("/bin/bash ");

    // CommandLine command = new CommandLine("/bin/sh ");

    //

command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node");

    CommandLine command = new CommandLine(

“/Applications/Appium.app/Contents/Resources/node/bin”);

    //

command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/
appium/build/lib/main.js");

    command.addArgument("appium");

    command.addArgument("--address");

    command.addArgument("0.0.0.0");

    command.addArgument("--port");

    command.addArgument("4723");

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

    command.addArgument("--log-level");

    command.addArgument("info");

    DefaultExecuteResultHandler resultHandler = new

DefaultExecuteResultHandler();

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(1);

    executor.execute(command, resultHandler);


    String appPath =

“/Users/khyati.dave/src/workspace/MobileTestAutomation/MoPubSampleApp.ipa”;

    File app = new File(appPath);

    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();

    desiredCapabilities.setCapability("platformName", "iOS");

    desiredCapabilities.setCapability("platformVersion", "9.3");

    desiredCapabilities.setCapability("deviceName", "iPhone 6");

    desiredCapabilities.setCapability("app", app.getAbsolutePath());

    driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"),

desiredCapabilities);

}

But it gives the error :

org.openqa.selenium.remote.UnreachableBrowserException: Could not start a
new session. Possible causes are invalid address of the remote server or
browser start-up failure.

Thanks Hassan : In my console I’m seeing the message " appium server
started " but my test is not launching the application and running the test
. I’m getting “unreachable browser exception. could not start a new session”
Here’s my code :

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/build/lib/main.js”,
false);

    command.addArgument("--address");

    command.addArgument("127.0.0.1");

    command.addArgument("--port");

    command.addArgument("4723");

    command.addArgument("--no-reset", true);

    command.addArgument("--log-level");

    command.addArgument("info");

    DefaultExecuteResultHandler resultHandler = new

DefaultExecuteResultHandler();

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(1);

    executor.execute(command, resultHandler);

    System.out.println("Appium server started");


    String appPath =

“/Users/khyati.dave/src/workspace/MobileTestAutomation/MoPubSampleApp.ipa”;

    File app = new File(appPath);

    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();

    desiredCapabilities.setCapability("platformName", "iOS");

    desiredCapabilities.setCapability("platformVersion", "9.3");

    desiredCapabilities.setCapability("deviceName", "iPhone 6");

    desiredCapabilities.setCapability("app", app.getAbsolutePath());

    driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"),

desiredCapabilities);

}


// close the connection after test

@After

public void teardown() {

    driver.quit();

}


@Test

public void test() throws InterruptedException {

    // add a new ad

    driver.findElement(By.xpath("//UIANavigationBar[1]/UIAButton[3]"

)).click();

    // click AD Type

    driver.findElement(By.xpath("//UIAWindow[2]/UIAButton[2]")).click();

}

@hassan : sorry its just printing out the line since i had added a print stmt but when i do a ps -ef | grep appium there is no process running. If I launch the appium app and click on launch wait for 200 ms message and then run the test only then the test runs.
I want to automate the step where I don’t need to "launch the appium app and click the "launch " button manually.

tldr: Just try those capabilities with an android device and start server in a console with appium command:

desired_caps = {
    caps: {
      platformName:          'Android',
      deviceName:            '072e96bdd007f9f0',
      newCommandTimeout:     0,  
      bundleId:              'com.company.game',
      app:                   "path/to/app.apk"
      fullReset:             false,
      noSign:                true
    },
    appium_lib: {
      port:                  4723
 }

I’m not sure if I understood what you are trying to achieve. I think you are trying some kind of “test launcher” which does all the steps.

I have a bash script which executes the appium server in background and then launch the client…

Anyway, it seems that you have everything correctly installed but something isn’t well configured. Have you ever reached launching a test manually? (If you didn’t try to start from the beginning: Just launch appium and get the “handshake” between client and server).

I have a setup with cucumber in ruby, I think it makes everything more logical to start.

I hope this can help you

I have used the following snippet for launching the appium server through java code in mac.

public void startAppium() throws ExecuteException, IOException, InterruptedException {
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);
Thread.sleep(50000);
System.out.println(“Appium Server starting”);
}

I’m calling the above method inside @BeforeTest. Inside @BeforeMethod I’m mentioning the desired capabilities. The server launching and it is taking the desired capabilities as well but it is showing the following log with error:

e[36minfoe[39m: e[37m–>e[39m e[37mPOSTe[39m e[37m/wd/hub/sessione[39m e[90m{“desiredCapabilities”:{“app”:"/Users/aaaa/dd/qd.apk",“appPackage”:“com.xxx.xxx”,“appActivity”:“com.xxx.xxx.MainActivity”,“newCommandTimeout”:“1200”,“platformName”:“Android”,“deviceName”:“Android Device”}}e[39m
e[36minfoe[39m: Client User-Agent string: Apache-HttpClient/4.5.2 (Java/1.8.0_73)
e[36minfoe[39m: [debug] Using local app from desired caps: /Users/aaaa/dd/qd.apk
e[36minfoe[39m: [debug] Creating new appium session 6a2245bd-d1ac-443c-bf5f-c6e2c2f64e22
e[36minfoe[39m: Starting android appium
e[36minfoe[39m: [debug] Getting Java version
e[36minfoe[39m: [debug] Cleaning up android objects
e[36minfoe[39m: [debug] Cleaning up appium session
e[31merrore[39m: Failed to start an Appium session, err was: Error: ‘java -version’ failed. Error: Command failed: /bin/sh -c java -version
/bin/sh: java: command not found

e[36minfoe[39m: [debug] Error: ‘java -version’ failed. Error: Command failed: /bin/sh -c java -version
/bin/sh: java: command not found

I have tried by setting the JAVA_HOME to environment variable of the project but it is of no use.

Can anyone please provide the suggestion on this.Thanks in advance.

@Hassan_Radi Hi, I am not finding appium.js file in the bin folder, rather its in the lib folder. While I run the command to open the appium server, I am getting the below error.

/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium/Contents/Resources/node_modules/appium/lib/appium.js
module.js:341
throw err;
^

Error: Cannot find module ‘/Applications/Appium/Contents/Resources/node_modules/appium/lib/appium.js’
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3

Please share your thoughts on this issue.

Please use this file instead:

/Applications/Appium/Contents/Resources/node_modules/appium/lib/main.js

Hi @Hassan_Radi,
I am using
Mac OS: Sierra(10.12.6)
Appium Version: 1.8.0 beta 3
Node Version: 8.5.0

There is no directory ‘node’ inside of /Applications/Appium.app/Contents/Resources/.
Only ‘app’ directory is present.

What should I do here ?
Thanks

I am also facing same problem which was faced by @prat3ik . Can someone please help.

There is no directory ‘node’ inside of /Applications/Appium.app/Contents/Resources/. Where we can find node file.

i am using the latest appium desktop and you can start your appium from java code by using the solution in the link.
check it here