DevOps is the current trend in IT for faster delivery of the ready product. During the whole pipeline process, automated test for desktop application will start automatically at the CI server without any trouble because it does not require pre-environment setup. But we need to turn up the Appium server before starting mobile test; so ideally there should be some medium available to trigger the server at the time of mobile test execution. Maybe a human intervention required. We can avoid manual intervention for triggering the server by using a technique to auto start Appium server at runtime for continuous delivery of the product.
That sounds cool?
In today’s agenda, we will discuss a technique to auto start the Appium server by using Java programming and with some inbuilt classes and methods inside the AppiumDriver interface. Hence, it is going to be a great learning journey for you.
Once the server gets started automatically then you can either test your mobile native apps or mobile web apps. Before we begin, we need to furnish some data which will be used in our program.
Pre-Requisites
- Install Appium with npm and get appium.js file’s path
Path: C:\Users\blogg\AppData\Roaming\npm\node_modules\appium\lib\appium.js
- Install node js and get the path of node.exe file
Path: C:\Program Files\nodejs\node.exe
- Create a text file to capture the logs from Appium server
These are simple pre-requisites only and I believe you would have this already configured.
But, before everything I highly recommend you following configuration to be done in your test system of CI:
- Install Appium with NodeJS
- Configure Appium in Eclipse with Selenium & Java
- A step by step guide to creating a Maven project in Jenkins
How to auto start Appium server using Java?
This is the agenda of this tutorial, so to auto start appium server we need to set all the properties like IP of the host, port number, etc, through Java programming. Here we basically instantiate AppiumServiceBuilder class and further, we set all the values in AppiumDriverLocalService.
See the demo code below:
AppiumServiceBuilder appSerBuilder = new AppiumServiceBuilder(); AppiumDriverLocalService appService = AppiumDriverLocalService.buildService(appSerBuilder.withAppiumJS(new File(appiumJsFile)) .usingDriverExecutable(new File(nodeExeFile)) .withLogFile(new File(appiumLogFile)) .withIPAddress("127.0.0.1") .usingPort(4723));
We use the following commands to start and stop the server respectively:
appService.start(); appService.stop();
We can take two approaches to auto start appium server. Let’s have a look at them to have a clear picture of everything.
Approach# 1 to Auto start Appium Server by passing all the values inside buildService(arg)
package AppiumPractice; import org.testng.annotations.Test; import io.appium.java_client.AppiumDriver; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; import org.testng.annotations.BeforeMethod; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterMethod; public class AutostartAppiumServerProgram2 { AppiumDriver<MobileElement> driver; DesiredCapabilities cap; AppiumDriverLocalService appService; String appiumJsFile = "C:\\Users\\blogg\\AppData\\Roaming\\npm\\node_modules\\appium\\lib\\appium.js"; String nodeExeFile = "C:\\Program Files\\nodejs\\node.exe"; String appiumLogFile = "AppiumLogs.text"; @Test public void testMethod() throws MalformedURLException { System.setProperty("webdriver.chrome.driver", "C:\\Appium\\\chromedriver.exe"); cap = new DesiredCapabilities(); cap.setCapability("deviceName", "vivo 1818"); cap.setCapability("udid", "d7206a48"); cap.setCapability("platformName", "Android"); cap.setCapability("platformVersion", "9"); cap.setCapability(CapabilityType.BROWSER_NAME, "Chrome"); URL url = new URL("https://127.0.0.1:4723/wd/hub"); driver = new AndroidDriver<MobileElement>(url,cap); driver.get("https://www.inviul.com"); System.out.println("Appium Test Started"); String title = driver.getTitle(); System.out.println("Title is- "+title); } @BeforeMethod public void startAppiumServer() { AppiumServiceBuilder appSerBuilder = new AppiumServiceBuilder(); appService = AppiumDriverLocalService.buildService(appSerBuilder.withAppiumJS(new File(appiumJsFile)) .usingDriverExecutable(new File(nodeExeFile)) .withLogFile(new File(appiumLogFile)) .withIPAddress("127.0.0.1") .usingPort(4723)); appService.start(); System.out.println("Service URL- "+appService.getUrl().toString()); } @AfterMethod public void tearDownDriver() { driver.close(); driver.quit(); appService.stop(); } }
Approach# 2 to Auto start Appium Server
package AppiumPractice; import org.testng.annotations.Test; import io.appium.java_client.AppiumDriver; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; import org.testng.annotations.BeforeMethod; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterMethod; public class AutostartAppiumServerProgram { AppiumDriver<MobileElement> driver; DesiredCapabilities cap; AppiumDriverLocalService appService; String appiumJsFile = "C:\\Users\\blogg\\AppData\\Roaming\\npm\\node_modules\\appium\\lib\\appium.js"; String nodeExeFile = "C:\\Program Files\\nodejs\\node.exe"; String appiumLogFile = "AppiumLogs.text"; @Test public void testMethod() throws MalformedURLException { System.setProperty("webdriver.chrome.driver", "C:\\Appium\\\chromedriver.exe"); cap = new DesiredCapabilities(); cap.setCapability("deviceName", "vivo 1818"); cap.setCapability("udid", "d7206a48"); cap.setCapability("platformName", "Android"); cap.setCapability("platformVersion", "9"); cap.setCapability(CapabilityType.BROWSER_NAME, "Chrome"); URL url = new URL("https://127.0.0.1:4723/wd/hub"); driver = new AndroidDriver<MobileElement>(url,cap); driver.get("https://www.inviul.com"); System.out.println("Appium Test Started"); String title = driver.getTitle(); System.out.println("Title is- "+title); } @BeforeMethod public void startAppiumServer() { AppiumServiceBuilder appSerBuilder = new AppiumServiceBuilder(); appSerBuilder.withAppiumJS(new File(appiumJsFile)) .usingDriverExecutable(new File(nodeExeFile)) .withLogFile(new File(appiumLogFile)) .withIPAddress("127.0.0.1") .usingPort(4723); appService = appSerBuilder.build(); appService.start(); System.out.println("Service URL- "+appService.getUrl().toString()); } @AfterMethod public void tearDownDriver() { driver.close(); driver.quit(); appService.stop(); } }
This was all about auto starting appium server with Java programming. It will help you when you want to trigger your test from the CI server, which stops human intervention. Feel free to share your feedback, ideas, and comments by using the form below.
Getting the below error when executing
FAILED CONFIGURATION: @BeforeMethod startAppiumServer
io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started. The given Node.js executable: C:\Program Files\nodejs\node.exe Arguments: [C:\Users\JohnDoe\AppData\Roaming\npm\node_modules\appium\build\lib\appium.js, –port, 4723, –address, 127.0.0.1, –log, C:\Users\JohnDoe\eclipse-workspace\testcodes\AppiumLogsJD.text]
at io.appium.java_client.service.local.AppiumDriverLocalService.start