Procedure to start and stop Appium server programmatically using Java code
Hi Guys
Here I am explaining the procedure to start and stop the appium server programmatically
I hope all the software required for mobile automation using appium are installed and they are running.
Step 1 : Find the path of node.exe where it is installed
Its good practice to install appium server in separate drive like "D://" or "E://" drive instead of "C://" drive
In my system, node.exe is installed in the following path
node_Exe = "D:\\Appium\\node.exe"
Step 2 : Find the path of Appium.js where it is installed
In my system, Appium.js is installed in the following path
appium_js = "D:\\Appium\\node_modules\\appium\\bin\\appium.js"
Step 3: Now we will write bunch of code to start appium server programmatically
Note :
## Start_Server() is method to start appium server
public void Start_Server() throws IOException, InterruptedException
{
## Here we combined the path of both node.exe and appium.js server and stored in String variable.
String Start_server="D:\\Appium\\node.exe D:\\Appium\\node_modules\\appium\\bin\\appium.js";
process = Runtime.getRuntime().exec(Start_server);
if(process!= null)
{
System.out.println("Started Appium Server");
}
else
{
System.out.println("NOT Started the Appium Server");
}
Thread.sleep(12000);
}
Step 4 : Now we will write the bunch of code to stop the appium server programmatically.
## Stop_Server() is method to stop appium server
public void Stop_Server() throws InterruptedException
{
if(process!=null)
{
process.destroy();
Thread.sleep(6000);
System.out.println("Stopped the appium Server");
}
}
Step 5: Now we will reuse above method to start and stop appium server programmatically using an example
1. now We will create one base class which consist of 3 methods
a. Start_Server()
b. Stop_Server()
c. Init_App() ## which initialise app
After combining all those our base class looks like this.
package Day4;
import io.appium.java_client.android.AndroidDriver;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Base_Class_drag_drop {
static Process process;
public static AndroidDriver driver;
//start the server
public static void Start_Server() throws IOException, InterruptedException
{
String Start_server="D:\\Appium\\node.exe D:\\Appium\\node_modules\\appium\\bin\\appium.js";
process = Runtime.getRuntime().exec(Start_server);
if(process!= null)
{
System.out.println("Started Appium Server");
}
else
{
System.out.println("NOT Started the Appium Server");
}
Thread.sleep(12000);
}
//Init app
public static void Init_app() throws MalformedURLException
{
DesiredCapabilities capabilities= new DesiredCapabilities();
capabilities.setCapability("deviceName","GT-I9300I");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("platformVersion","4.4.4");
capabilities.setCapability("appPackage","com.mobeta.android.demodslv");
capabilities.setCapability("appActivity","com.mobeta.android.demodslv.Launcher");
driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
//Stop server
public static void Stop_Server() throws InterruptedException
{
if(process!=null)
{
process.destroy();
Thread.sleep(6000);
System.out.println("Stopped the appium Server");
}
}
}
Step 6 : With the help of Base class we will start and stop the appium server programmatically and also perform drag and drop operation on APK
Entire code will look like this.
package Day4;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
public class driver_drag_drop extends Base_Class_drag_drop {
public static void main(String[] args) throws IOException, InterruptedException {
## This call is calling Start_Server() method which will start appium server programmatically
Start_Server();
## This call is calling Init_app() method which will initialise the app i.e app will open on mentioned device.
Init_app();
## Here we are performing actual action
WebElement ele = driver.findElementById("com.mobeta.android.demodslv:id/activity_title");
ele.click();
List<WebElement> drag_ele = driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle");
System.out.println(drag_ele.size());
//drag and drop use Touchaction
TouchAction action= new TouchAction(driver);
action.longPress(drag_ele.get(0)).moveTo(drag_ele.get(5)).release().perform();
Thread.sleep(8000);
action.longPress(drag_ele.get(6)).moveTo(drag_ele.get(1)).release().perform();
Thread.sleep(4000);
## This call is calling Stop_Server() method which will stop appium server programmatically
Stop_Server();
}
PS: I hope you people enjoyed the blog.