Start & Stop Appium Server Programmatically using Java (MAC OSX)

Appium is widely used for mobile automation these days. You have to have your Appium server up and running before you can run your automated Appium tests. Most of the times, the test engineers ensure that the Appium server is running before they start executing the test suite. But this arrangement won’t work if you want to run your Appium tests in Continuous Integration/Deployment.

In this post, we will see how to start & stop Appium server programmatically using Java. This code is for Mac OSX (10.10). In next article we will see how to start & stop Appium server programmatically using Java on windows.

So here is the code Create a class called AppiumServer and paste the code mentioned below

In your test class, before you set driver capabilities, start the Appium server like the following

Appiumserver server=new Appiumserver(); 
System.out.println("---- Starting Appium server ----"); 
server.startServer(); 
//Code for setting desired capabilities goes here (after the server starts successfully) 

Hope you found this useful. If you like this post or found it useful, don’t forget to share it. If you face any difficulties in getting this up and running, kindly comment below and we will try our best to respond.

Comments

  1. Hi Quick Question

    With this method we can start the Appium server. How do we give it the iOS settings for example that you do through the GUI?

    Or can we set the settings in the Appium GUI and it will set them, and then we can just start the server from the java code.

    Thanks

    ReplyDelete
    Replies
    1. Which settings you are talking about, in particular? What we have done is we have separate class that takes care of server arguments. And we can pass all the arguments that you can pass the GUI. Feel free to reach me in case you need further information or help. Happy to help! :)

      Delete
  2. Thanks for this code .. was really helpful and success

    ReplyDelete
  3. hi there,

    thank you for sharing valuable information.

    Would please help me as I'm newbie to appium and struggling a lot to run the server programatically

    I have used the code but when I run it... got the below error

    module.js:341
    throw err;
    ^

    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

    is there anything you can help me?

    Thank you

    ReplyDelete
  4. hi there,

    Thank you for sharing valuable information

    I am a newbie to appium and struggling to launch appium programatically when I tried your code I end up with below error

    module.js:341
    throw err;
    ^

    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

    Am I missing anything? Please help

    thank you

    ReplyDelete
    Replies
    1. Hi Srinivas mit,

      The error clearly mentions that it cannot find the appium.js file on the specified location.


      If you see .. in startServer()
      there is a line that says

      command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js",false);

      Just replace the path to appium.js to where ever you have installed Appium on your machine.

      Kindly let me know if this helps or you need more information.

      With thanks,
      Right QA Team

      Delete
  5. Tried this but getting error -

    org.openqa.selenium.WebDriverException: Connection refused (Connection refused)

    Connection refused (Connection refused)
    Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
    System info: host: 'Tanus-MacBook-Pro.local', ip: 'fe80:0:0:0:873:8972:6ac7:2835%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.5', java.version: '1.8.0_151'
    Driver info: driver.version: IOSDriver
    at io.appium.java_client.remote.AppiumCommandExecutor.lambda$2(AppiumCommandExecutor.java:243)
    at java.util.Optional.orElseGet(Optional.java:267)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:242)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.ios.IOSDriver.execute(IOSDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:130)
    at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:38)
    at io.appium.java_client.AppiumDriver.(AppiumDriver.java:84)
    at io.appium.java_client.AppiumDriver.(AppiumDriver.java:94)
    at io.appium.java_client.ios.IOSDriver.(IOSDriver.java:95)
    at managers.DriverConnection.(DriverConnection.java:28)
    at managers.DriverConnection.getDriverInstance(DriverConnection.java:37)

    ReplyDelete
  6. hey, I tried using this guide but i get getting Connection Refused any idea how to fix that ?

    ReplyDelete

Post a Comment