Run appium server programmatically using java

As we already got to know from our earlier post Appium test run,  always run appium server  / appium GUI before running your appium test.

While running automated tests, we have to manually run the appium server / GUI first.

Problem:

let’s say that we have to schedule our appium tests to run (may be a nightly build test) and we won’t be at desk or office to run the appium server first.

Solution:

We can write a java code block to run the appium server before running our actual appium test, then we need to do anything manually.

Prerequisites:

  • Download and install appium.exe (refer step6 of appium setup)
  • Install node.exe (refer step7 of appium setup)
  • Jar file – you may get error while running the appium programmatically, validator or lang3 error, include these below jars in to your project

commons-validator-x.x.x.jar – download here

commons-lang3-x.x.jar – download here

Let’s see how we can achieve this – 

below code will automatically run the appium server and after run the appium test.

package test;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

public class AppiumServer {
static DesiredCapabilities cap;
//static AppiumDriver driver;
static AndroidDriver driver;

public static void main(String[] args) throws MalformedURLException {
AppiumDriverLocalService service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder().usingDriverExecutable(new File(“C:\\Program Files\\nodejs\\node.exe”))
.withAppiumJS(new File(“C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js”))
.withLogFile(new File(“./AppiumServerlog.txt”)));
service.start();

cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PLATFORM, “Android”);
cap.setCapability(CapabilityType.VERSION, “5.1.0”);
cap.setCapability(“deviceName”, “mygeny510”);
cap.setCapability(“appPackage”, “com.android.dialer”);
cap.setCapability(“appActivity”, “com.android.dialer.DialtactsActivity”);
driver = new AndroidDriver<MobileElement>(new URL(“http://127.0.0.1:4723/wd/hub&#8221;), cap);
driver.quit();

service.stop();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s