SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Start-Stop Appium Server Programmatically - Method 2

Earlier In PREVIOUS POST, I have described you one of the example to start and stop appium server programmatically in your android software app automation testing with practical example. Now let me explain you another method to start and stop appium server programmatically. You can use any these two methods to start and stop appium server In your appium software automation test.

App To Use And Aim To Achieve
We will use same Api Demos android software app In this test. Also our aim Is to start and stop appium server programatically. In this test, We will use CommandLine class of apache to execute commands for starting and stopping appium server during software automation test execution.

Create And Run Test
Create new AppiumStartStop.java file In Android package of your project In eclipse and use bellow given test script In It.

Note : In bellow given appium software automation test script, If your node.exe and appium.js are located at any other path then please update values of nodePath and appiumJSPath string variables accordingly.

AppiumStartStop.java
package Android;

import io.appium.java_client.android.AndroidDriver;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AppiumStartStop {
 static AndroidDriver driver;
 // Set path of your node.exe file. Set your path.
 // Progra~1 represents Program Files folder.
 String nodePath = "C:/Progra~1/Appium/node.exe";
 // Set path of your appium.js file. Set your path.
 String appiumJSPath = "C:/Progra~1/Appium/node_modules/appium/bin/appium.js";

 // This method Is responsible for starting appium server.
 public void appiumStart() throws IOException, InterruptedException {
  // Created object of apache CommandLine class.
  // It will start command prompt In background.
  CommandLine command = new CommandLine("cmd");
  // Add different arguments In command line which requires to start appium server.
  command.addArgument("/c");
  command.addArgument(nodePath);
  command.addArgument(appiumJSPath);
  //Set Server address
  command.addArgument("--address");
  command.addArgument("127.0.0.1");
  //Set Port
  command.addArgument("--port");
  command.addArgument("4723");
  command.addArgument("--no-reset");
  command.addArgument("--log");
  //Set path to store appium server log file.
  command.addArgument("D://appiumLogs.txt");
  // Execute command line arguments to start appium server.
  DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
  DefaultExecutor executor = new DefaultExecutor();
  executor.setExitValue(1);
  executor.execute(command, resultHandler);
  // Wait for 15 minutes so that appium server can start properly before going for test execution.
  // Increase this time If face any error.
  Thread.sleep(15000);
 }

 // This method Is responsible for stopping appium server.
 public static void appiumStop() throws IOException {
  // Add different arguments In command line which requires to stop appium server.
  CommandLine command = new CommandLine("cmd");
  command.addArgument("/c");
  command.addArgument("taskkill");
  command.addArgument("/F");
  command.addArgument("/IM");
  command.addArgument("node.exe");
  // Execute command line arguments to stop appium server.
  DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
  DefaultExecutor executor = new DefaultExecutor();
  executor.setExitValue(1);
  executor.execute(command, resultHandler);
 }

 @BeforeTest
 public void setUp() throws Exception {
  // Start appium server.
  appiumStart();
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "io.appium.android.apis");
  capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void ScrollToTab() {
  // Scroll till element which contains "Views" text If It Is not visible on screen.
  driver.scrollTo("Views");
  // Click on Views.
  driver.findElement(By.name("Views")).click();
 }

 @AfterTest
 public void End() throws IOException {
  driver.quit();
  // Stop appium server when test Is ended.
  appiumStop();
 }
}

Test Script Description
If you see In test script, I have created appiumStart() and appiumStop() methods which are responsible to start and stop appium server during software test script execution. We have set few command line arguments and executed them to start and stop appium server.

This way you can start and stop appium server programmatically.

No comments:

Post a Comment