When we are automating the mobile application and set cron jobs at different time intervals then we do not start and stop the appium server manually. We should have some code that will run before our testcases ie they should be executed in the beginning of our testsuite. If we are using testNG as the testing framework then we should put the method to start the server in "@BeforeSuite" annotation and the method to stop the server in the "@AfterSuite" annotation.
There are basically two files, we need to start the Appium server:
The code to start and stop Appium server is:
public class TestAppiumServer {
// Launch Appium on Windows
private static Process process;
//Calling the node.exe and appium.js
private static String STARTSERVER = "C:\\Program Files (x86)\\Appium\\node.exe C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js"; //Give the path of these two files as per your system setting.
//Starting the Appium Server
public static void startAppiumServer() throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(STARTSERVER);
Thread.sleep(7000);
if (process != null) {
System.out.println("Appium server started");
}
}
//Stopping the Appium Server
public static void stopAppiumServer() throws IOException {
if (process != null) {
process.destroy();
}
System.out.println("Appium server stopped");
}
public static void main(String[] args) throws IOException, InterruptedException {
stopAppiumServer();
startAppiumServer();
Thread.sleep(5000);
stopAppiumServer();
}
}
In the above code I first stop the server to confirm that no instance of the server in the running stop and then start the server again.
0 Comment(s)