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.
1. java-client.jar
2. Selenium-server-standalone.jar
3. commons-validator.jar
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 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;
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:
2. Selenium-server-standalone.jar
3. commons-validator.jar
Example with buildDefaultService:
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();
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();
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class AppiumServerTest {
public static void main(String[] args) throws IOException {
String filePath;
String appium_url = "appium -a 0.0.0.0 -p 4723";
Process p;
if(System.getProperty("os.name").toLowerCase().contains("mac")){
filePath ="//usr//local//bin//appium.sh";
FileWriter filewriter = new FileWriter(filePath);
BufferedWriter bufferwriter = new BufferedWriter(filewriter);
bufferwriter.write(appium_url);
bufferwriter.close();
p = Runtime.getRuntime().exec("chmod u+x "+filePath);
p = Runtime.getRuntime().exec("/usr/bin/open -a Terminal "+filePath);
}
else if(System.getProperty("os.name").toLowerCase().contains("window")){
p = Runtime.getRuntime().exec("cmd /c start cmd.exe /K \""+appium_url);
}
System.out.println("Appium Server Started");
}
}
No comments:
Post a Comment