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.
Hi Anuja. Thanks for your example. But i am facing an issue using your code and running the test using maven command.
ReplyDelete"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
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.
Deleteservice = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
usingPort(2856).usingDriverExecutable(new File(Appium_Node_Path)).
I have started Appium through NODE...the below is from jenkins..
ReplyDeleteCOMMAND 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
Works like charm after resolving dependencies on few libraries. Thanks a lot.
ReplyDeletei have installed appium 1.6 using npm but programmatically it still opens up appium 1.4 ..how could i resolve
ReplyDeletecan i use this appium for web based application and how?
ReplyDeleteHi , 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)
DeleteHi Anuja,
ReplyDeleteCould please you make the same tutorial for MacOS ?
Thank you in advance.
There is a slight changes happened as per new appium server Appium v1.7.0
ReplyDelete** 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);
}
Yes this site is very useful.....i was struggling from long time to install appium before i browsed into this site.
ReplyDeleteimport { ipcMain, BrowserWindow, Menu } from 'electron';
ReplyDelete^^^^^^
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
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.
ReplyDeleteWow 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.
ReplyDeleterpa training in velachery| rpa training in tambaram |rpa training in sholinganallur | rpa training in annanagar| rpa training in kalyannagar
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.
ReplyDeleteBest Devops online Training
Online DevOps Certification Course - Gangboard
Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.
ReplyDeleteaws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore
Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.
ReplyDeleteaws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore
I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
ReplyDeleteaws training in chennai | aws training in annanagar | aws training in omr | aws training in porur | aws training in tambaram | aws training in velachery
Thank you for sharing the great information.it is really nice. Java training in Chennai | Certification | Online Course Training | Java training in Bangalore | Certification | Online Course Training | Java training in Hyderabad | Certification | Online Course Training | Java training in Coimbatore | Certification | Online Course Training | Java training in Online | Certification | Online Course Training
ReplyDeleteThis was nice and amazing and the given contents were very useful and the precision has given here is good.
ReplyDeleteBest Data Science Training Institute in Pune
Data Science Training Institute in Bangalore
Data Science Certification Course in Bangalore