We can start and stop appium server without having a GUI in 3 different ways.
1. By using AppiumDriverLocalService with buildDefaultService
2. By using AppiumDriverLocalService with customized buildService
3. By using command through CMD/Terminal
To start/stop appium server by using this approach, appium has to be installed in your machine. If it is not installed, install it by following the below links.
Pre-Requisites:
1. java-client.jar
2. Selenium-server-standalone.jar
3. commons-validator.jar
Example with buildDefaultService:
Let us see the sample program to run appium server programatically with AppiumDriverLocalService. In this approach driver will invoke with the default address and port .
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class MobileBrowserTest {
DesiredCapabilities capabilities;
AppiumDriverLocalService appiumServer;
static AppiumDriver driver = null;
@BeforeSuite
public void setup(){
// Start Appium Server
appiumServer = AppiumDriverLocalService.buildDefaultService();
appiumServer.start();
capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Nexus_Emulator");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");
capabilities.setCapability(CapabilityType.VERSION, "8.0");
capabilities.setCapability("platformName", "android");
try {
String hubURL = appiumServer.getUrl().toString();
driver = new AndroidDriver<>(new URL(hubURL), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
Assert.fail("Failed to create a session, Check the appium adress and port");
}
}
@Test
public void TC1(){
driver.get("https://www.google.co.in");
driver.findElement(By.name("q")).sendKeys("Welcome");
}
@AfterSuite
public void tearDown(){
if(driver!=null)
driver.quit();
//Stop Appium server
appiumServer.stop();
}
}
Example with buildService:
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.AppiumDriver;
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.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class MobileBrowserTest {
DesiredCapabilities capabilities;
AppiumDriverLocalService appiumServer;
AppiumServiceBuilder serviceBuilder;
static AppiumDriver driver = null;
@BeforeSuite
public void setup(){
//Store appium console log in txt file
File logFile = new File("E:\\Docs\\AppiumLogFile.txt");
capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Nexus_Emulator");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");
capabilities.setCapability(CapabilityType.VERSION, "8.0"); capabilities.setCapability("platformName", "android");
// Start Appium Server
serviceBuilder = new AppiumServiceBuilder()
.withIPAddress("0.0.0.0")
.usingAnyFreePort()
.withCapabilities(capabilities)
.withLogFile(logFile);
appiumServer = AppiumDriverLocalService.buildService(serviceBuilder);
appiumServer.start();
try {
String hubURL = appiumServer.getUrl().toString();
driver = new AndroidDriver<>(new URL(hubURL), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
Assert.fail("Failed to create a session, Check the appium adress and port");
}
}
@Test
public void TC1(){
driver.get("https://www.google.co.in");
driver.findElement(By.name("q")).sendKeys("Welcome");
}
@AfterSuite
public void tearDown(){
if(driver!=null)
driver.quit();
//Stop Appium server
appiumServer.stop();
}
}