Starting Appium server programmatically on MAC

@sunny_sachdeva

You need to change this line to refer to the location of the 1.4.12 package.

Hi Hasan,

I tried below command

/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 4725 --no-reset --local-timezone

but Appium server still listens at port 4723

/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 4725 --no-reset --local-timezone
info: Welcome to Appium v1.4.13 (REV c75d8adcb66a75818a542fe1891a34260c21f76a)
info:

Appium REST http interface listener started on 127.0.0.1:4723

info: [debug] Non-default server args: {“address”:“127.0.0.1”,“bootstrapPort”:4725,“noReset”:true,“localTimezone”:true,“chromeDriverPort”:9516}
info: Console LogLevel: debug

@Venkatesh

This doesn’t change the server port number, use this to change the server port number

–port 4725

Also it would be better if you don’t write any code at all and use my “Appium Support” library to take care of that: Home · Genium-Framework/Appium-Support Wiki · GitHub

1 Like

I get the below error when i use port

appium.js: error: Unrecognized arguments: —port 4725.

my bad i used — instead of – :(, works now thanks Hassan.

Hey guys,
So I have another Mac I try to work with to start an Appium session. While with the first Mac it works well the newer doesn’t allow me to initiate the Appium programmatically. Here’s the code I am using:
ssh.RunCommand("/usr/local/bin/forever start /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 0.0.0.0 --port 4723 --log-level debug --device-name \"iPhone 6\" --platform-name iOS --platform-version \"8.4\" --app \"/Applications/my.app\" --browser-name iOS -l --log /Library/Logs/Appium/current.log");

For some reason it tells me: “env: node: No such file or directory”
Now, I am guessing that there’s some problem running the “forever” file. But I am not sure what stops it from running properly…
Thanks!

Hello Friends,

Please try the code that I posted in my post http://aksahu.blogspot.in/2015/10/start-and-stop-appium-server.html

Hope it will help you!

Thanks,
Aswini Kumar

I am having an issue getting appium to run from the terminal:

Automation-MacBook-Pro:~ testingpractice$ '/Applications/Appium.app/Contents/Resources/node/bin/node'
> /Applications/Appium.app/Contents/Resources/node_modules/appium.js
SyntaxError: Invalid flags supplied to RegExp constructor 'Appium'
    at new RegExp (native)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:132:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
> /Applications/Appium.app/Contents/Resources/node_modules/appium.js --address 127.0.0.1 --port 4723 --command-timeout "7200" --debug-log-spacing --platform-version "9.2" --platform-name "iOS" --show-ios-log --default-device --native-instruments-lib
SyntaxError: Unexpected identifier
    at Object.exports.createScript (vm.js:44:10)
    at REPLServer.defaultEval (repl.js:117:23)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
    at ReadStream.onkeypress (readline.js:109:10)

I can’t figure out what’s going wrong here. At the moment I am not trying to do this from within code. I am just manually trying to get the command to work in the terminal. Once I get the syntax of the command down I plan to execute it from my C# test via SSH.NET from a Windows PC.

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.