Sunday, 31 January 2016

How to Start Appium Server using Java Code?



 We have seen how to start and stop Appium server manually and now in this post we will come to know how to start and stop Appium server programmatically. Appium provide APIs so that you can start the server before running your test case or test suit and then you can stop the server once the execution is over.

Once you install appium you the files node.exe and appium.js will be there is your system. You need the path of these two files for starting apium server programmatically. Then you can copy and paste the below code. Call appiumStart() in @BeforeClass or @BeforeTest .

Required Jar files are commons-validator-1.4.1.jar and java-client-3.2.0.jar which you can download from here.

Video -



Explanation about the code -

AppiumDriverLocalService class provied the api to start and stop the server hence we have used this in our code below.usingPort() method is used to provide port number for starting the server. We ned to pass our node.exe path to method usingDriverLocation() and appium.js path to withAppiumJs() method. Then start() and stop() methods are used to start and stop the server. We need to use getUrl() method to get and pass the url while setting up the Desired Capabilities.




import java.io.File;

import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

public class AppiumServerStartStop {

    static String Appium_Node_Path="C:\\Program Files (x86)\\Appium\\node.exe";
    static String Appium_JS_Path="C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js";
    static AppiumDriverLocalService service;
    static String service_url;

    public static void appiumStart() throws Exception{
        service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
                usingPort(2856).usingDriverExecutable(new File(Appium_Node_Path)).
                withAppiumJS(new File(Appium_JS_Path)));
        service.start();
        Thread.sleep(25000);
        service_url = service.getUrl().toString();
    }

    public static void appiumStop() throws Exception{
        service.stop();

    }
}

Note use the service_url like shown below while setting up Desired Capabilities-

AppiumDriver driver= new AndroidDriver(new URL(service_url),cap);

If you find this Post useful do share with your friends and if you have some questions or suggestions do share them with me in the comments section below.Please follow QA Automated for latest updates.


 

18 comments:

  1. Hi Anuja. Thanks for your example. But i am facing an issue using your code and running the test using maven command.
    "Couldn't start Appium REST http interface listener. Requested port is already in use. PLease make sure there's no other instance of appium running already."

    Please help me if you know the fix. Thanks in advance

    ReplyDelete
    Replies
    1. This means your appium server instance is runnig with same port. You can try with changing port number. make the change in below line of code.
      service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
      usingPort(2856).usingDriverExecutable(new File(Appium_Node_Path)).

      Delete
  2. I have started Appium through NODE...the below is from jenkins..

    COMMAND LINE
    /usr/local/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/build/lib/main.js --address 0.0.0.0 --port 4723/wd/hub

    how do i start and make the tests run...as of now it just starts appium and does nothing :(


    Started by user anonymous
    [EnvInject] - Loading node environment variables.
    Building in workspace /Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/​
    [​] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/hudson6798240980173597906.sh
    + cd /Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/
    + chmod g+w+x appium.sh
    + sh appium.sh
    [Appium] Welcome to Appium v1.5.3
    [Appium] Non-default server args:
    [Appium] app: '/Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/src/T/result/MeasuringMaster.app'
    [Appium] Deprecated server args:
    [Appium] --app => --default-capabilities '{"app":"/Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/src/T/result/MeasuringMaster.app"}'
    [Appium] Default capabilities, which will be added to each request unless overridden by desired capabilities:
    [Appium] app: '/Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/src/T/result/MeasuringMaster.app'
    [Appium] Appium REST http interface listener started on 0.0.0.0:4723

    ReplyDelete
  3. Works like charm after resolving dependencies on few libraries. Thanks a lot.

    ReplyDelete
  4. i have installed appium 1.6 using npm but programmatically it still opens up appium 1.4 ..how could i resolve

    ReplyDelete
  5. can i use this appium for web based application and how?

    ReplyDelete
    Replies
    1. Hi , you can use appium to test mobile application having webviews plus you can test web applications in mobile browser check out my appium tutorial for How to Test Web Views using Appium (http://www.qaautomated.com/2016/02/testing-web-views-with-appium.html)

      Delete
  6. Hi Anuja,
    Could please you make the same tutorial for MacOS ?
    Thank you in advance.

    ReplyDelete
  7. There is a slight changes happened as per new appium server Appium v1.7.0

    ** No need to specify the Android js path only Node js is enough.

    code is as below.

    @BeforeTest
    public void appiumStart() throws Exception
    {
    service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
    .usingDriverExecutable(new File(Appium_Node_Path))
    .usingPort(4723));
    service.start();
    Thread.sleep(25000);
    System.out.println("---- Service Started-----");
    service_url = service.getUrl().toString();

    DesiredCapabilities capabilities=DesiredCapabilities.android();
    capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,BrowserType.CHROME);
    capabilities.setCapability(MobileCapabilityType.PLATFORM,Platform.ANDROID);
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"4d00a95f4fde3117");
    capabilities.setCapability(MobileCapabilityType.VERSION,"6.0.1");
    driver= new AndroidDriver(new URL(service_url),capabilities);
    }

    ReplyDelete
  8. Yes this site is very useful.....i was struggling from long time to install appium before i browsed into this site.

    ReplyDelete
  9. import { ipcMain, BrowserWindow, Menu } from 'electron';
    ^^^^^^
    SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
    at startup (bootstrap_node.js:150:9)
    this is the error i am getting while compiling the code...can you help me resolve it

    ReplyDelete
  10. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Java Online Training from India . or learn thru Online Training mode Hibernate Online Training . Nowadays Hibernate ORM has tons of job opportunities on various vertical industry. or learn thru JavaScript Online Training from India. Nowadays even JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  11. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
    Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
    Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
    AWS training in bangalore | Best aws training in bangalore

    ReplyDelete
  12. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa training in velachery| rpa training in tambaram |rpa training in sholinganallur | rpa training in annanagar| rpa training in kalyannagar

    ReplyDelete
  13. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  14. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  15. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  16. I am looking for and I love to post a comment Python classes in punethat "The content of your post is awesome" Great work!

    ReplyDelete