Saturday, August 31, 2019

Appium - How to start and stop Appium server programmatically | Mobile Automation



Appium - How to start and stop Appium server programmatically| Mobile Automation:


There are multiple ways to start Appium server. One way is to start by clicking Appium.exe by manually. But in most of the cases like in Jenkins we don’t want start Appium server manually, in that case it is better to start and stop Appium server via program.

Let us learn how to start appium server via program.

To start Appium server:


       
AppiumDriverLocalService appiumService;
String appiumUrl;

appiumService = AppiumDriverLocalService.buildDefaultService();
                              appiumService.start();
                              appiumUrl = appiumService.getUrl().toString();
To stop Appium server:


appiumService.stop();
Let us see the complete code to start and stop server with Native app example:

package practise;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

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;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;

public class StartAndStopAppiumServer {

              
               AndroidDriver<MobileElement> driver;
    AppiumDriverLocalService appiumService;
    String appiumUrl;
   
               @BeforeTest
               public void setUpAndStartServer() throws MalformedURLException{
                             
        appiumService = AppiumDriverLocalService.buildDefaultService();
                              appiumService.start();
                              appiumUrl = appiumService.getUrl().toString();
        System.out.println("Appium Service URL Address : - "+ appiumUrl);
       
                              DesiredCapabilities capabilities = new DesiredCapabilities();
                              capabilities.setCapability("deviceName", "emulator-5554");
                              capabilities.setCapability("platformName", "Android");
                              capabilities.setCapability("appPackage", "com.android.calculator2");
                              capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
                             
                              driver = new AndroidDriver<MobileElement>(new URL(appiumServiceUrl), capabilities);
                              driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
               }

               @Test
               public void Sum() {
                              System.out.println("Calculate sum of two numbers");
                              MobileElement oneBtn=driver.findElement(By.id("com.android.calculator2:id/digit_1"));
                              oneBtn.click();
                              MobileElement plusBtn=driver.findElement(By.id("com.android.calculator2:id/op_add"));
                              plusBtn.click();
                              MobileElement fiveBtn=driver.findElement(By.id("com.android.calculator2:id/digit_5"));
                              fiveBtn.click();
                              MobileElement equalToBtn=driver.findElement(By.id("com.android.calculator2:id/eq"));
                              equalToBtn.click();
               }

               @AfterTest
               public void End() {
                              System.out.println("Stop driver");
                              driver.quit();
                              System.out.println("Stop appium service");
                              appiumService.stop();
               }

}

Run this program as TestNg Test:

Output in mobile:



Console Output:




TestNg Output:


1 comment:

If any suggestions or issue, please provide