Start & Stop Appium Server
Start & Stop Appium Server
This guide explains how to Start and Stop Appium server programmatically (i.e., using code) in a simple way. Appium is an open-source tool that lets you write automated tests for mobile apps—whether they’re built for iOS or Android. Think of it like a robot that can tap buttons, enter text, and check if your app behaves correctly, just like a real user would.
Appium Server
Appium works by communicating with your mobile app through a server, which acts as a middleman between your test code and the device or emulator.
The Appium Server is a background program that listens for commands from your test scripts and carries them out on your mobile device or emulator. Before you can run any automated test with Appium, this server must be running. Normally, you’d start it manually from the command line or using a desktop app—but for smoother automation, especially in continuous testing environments, it’s better to start and stop it directly from your code.
Start Appium Server
To start the Appium Server programmatically in Java, you can use the AppiumServiceBuilder class from the Appium Java client library.
Sample Java Code snippet:
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
public class AppiumServerManager {
private static AppiumDriverLocalService service;
public static void startAppiumServer() {
service = new AppiumServiceBuilder()
.withIPAddress("127.0.0.1")
.usingPort(4723)
.build();
service.start();
System.out.println("Appium Server started on port 4723");
}
}
This code creates and starts an Appium server instance on your local machine at the default port 4723.
Stop Appium Server
To stop the server cleanly after your tests finish, call the stop() method on the same service instance:
public static void stopAppiumServer() {
if (service != null && service.isRunning()) {
service.stop();
System.out.println("Appium Server stopped");
}
}
Always check if the server is running before stopping it to avoid errors. You can call startAppiumServer() in your test setup (e.g., @BeforeSuite) and stopAppiumServer() in your cleanup (e.g., @AfterSuite).