How to launch and Stop Appium programmatically

How to launch and Stop Appium programmatically, How to start appium server programmatically using java code, There are different ways to start Appium server, every time starting manually would become time consuming process for testing approach, we can start using command line as well as using java program. In this lesson let us focus on How to start appium server using java program with very simple example. Many Software Testers working on GUI based Appium Server versions, Using GUI based Appium Tool easy to launch just click on Run icon, then it will start Appium Server as well as easy to stop the server by click on Stop icon but it is recommended to start or launch your Appium server using non GUI mode using Java programmatically for ease of use and easy to control while test execution.

Also Read : Start Learning APPIUM

How to launch and Stop Appium programmatically


We can start appium server and stop appium service using program with the help of below lines of codes.

For Start Apium Service = appiumservice.start() Before Test cases execution starts.
For Stop Appium Service = appiumservice..stop() Once Test Execution completes,which will stop the service.


How to launch and Stop Appium programmatically


In order to perform above task we require Appium Java Client i.e "AppiumDriverLocalService" class which extends Selenium Remote Service Driver Class to get start and stop methods.

public final class AppiumDriverLocalService extends org.openqa.selenium.remote.service.DriverService

Also Read: Selenium Grid APPIUM Configuration

AppiumServiceBuilder : This class is useful in build Appium Service,which is useful in set Server Address,port number,desired capabilities etc before starting the appium service.

Simple Implementation:

Below is the simple code to start and stop the Appium service using AppiumDriverLocalService  class

AppiumDriverLocalService driverService = AppiumDriverLocalService.buildDefaultService();
driverService.start();
//To Stop the service
driverService.stop();

Pre Requisites:

1.Appium Should be installed
2.Node js should be installed
3.commons-lang3-3.7 jar should be added to build path
4.commons-validator-1.6 should be added to build path


Complete Appium Program:

package com.android.testing;
import java.io.File;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.GeneralServerFlag;


public class AppiumService {

private AppiumDriverLocalService appiumService;
private DesiredCapabilities capabilities;
private AppiumServiceBuilder builder;
String node_js_Path="C:\\Program Files (x86)\\Appium\\node.exe";

@BeforeClass
public void startServer(){

//Set Capabilities
capabilities = new DesiredCapabilities();
capabilities.setCapability("AppiumserviceReset", false);

//Lets Build the Appium Service
builder = new AppiumServiceBuilder();
builder.usingDriverExecutable(new File(node_js_Path));
builder.withAppiumJS(new File("C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js"));
builder.withArgument(GeneralServerFlag.LOG_LEVEL,"info");
builder.usingAnyFreePort();
builder.withIPAddress("127.0.0.1");

//Now start the service
appiumService = AppiumDriverLocalService.buildService(builder);
appiumService.start();
}

@AfterClass
public void stopAppiumService(){
appiumService.stop();
}
@Test
public void checkAppiumService(){
System.out.println("Testing Appium Service");
}

}



Once you execute the code your Appium service will start on provided ip address and it'll display results in console as below and provide your valuable comments on this posts and please share with your friends.

Test Results:

[RemoteTestNG] detected TestNG version 6.9.6
[TestNG] Running:
  C:\Users\307980\AppData\Local\Temp\testng-eclipse-735127911\testng-customsuite.xml

[36minfo[39m: Welcome to Appium v1.4.13 (REV c75d8adcb66a75818a542fe1891a34260c21f76a)
[36minfo[39m: Appium REST http interface listener started on 127.0.0.1:14179
[36minfo[39m: Console LogLevel: info
[36minfo[39m: [37m-->[39m [37mGET[39m [37m/wd/hub/status[39m [90m{}[39m
[36minfo[39m: [37m<-- GET /wd/hub/status [39m[32m200[39m[90m 18.825 ms - 105[39m [90m{"status":0,"value":{"build":{"version":"1.4.13","revision":"c75d8adcb66a75818a542fe1891a34260c21f76a"}}}[39m
Testing Appium Service
PASSED: checkAppiumService

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

No comments:

Post a Comment