Appium Concepts :Difference between “.activateApp” Vs “.terminateApp” Vs “.removeApp” (Appium Built-In methods)

Get a step-by-step walkthrough in the video below!
Inthis Article, We will discuss the Uses & differences between “.activateApp” Vs “.terminateApp” Vs “.removeApp” (Appium Built-In methods)
Appium Built-In Method : — “.activateApp(appId)”
driver.activateApp(appId);
Description:
This method starts/opens an already running app by its ID. i.e. Activates an app, if it is not running or it is running in the background
- Purpose: Brings the specified app to the foreground.
- Effect: If the app is running in the background, it is resumed. If it is not running, it is launched.
- Use Case: Useful for switching between apps or relaunching the app after it has been backgrounded or terminated (but not uninstalled).
Example (Appium):
//Case-1: Start an wdiodemo app
driver.activateApp("com.wdiodemoapp");
System.out.println("Started wdiodemo app successfully");
Thread.sleep(5000);
//Case-2: Open the wdiodemo app from background
driver.activateApp("com.wdiodemoapp");
System.out.println("Opened the wdiodemo app successfully");
Appium Built-In Method : — “
.terminateApp(appId)
”
driver.terminateApp(appId);
Description:
This method is primarily used to forcefully close an application on the device.
- Purpose: Terminates the app process on the device (similar to force closing it).
- Effect: The app is closed, but not uninstalled from the device. The app’s data may be retained depending on the platform.
- Use Case: Useful for testing how the app behaves after being killed and relaunched.
Example (Appium):
//Close the wdiodemo app
driver.terminateApp("com.wdiodemoapp");
System.out.println("Close the wdiodemo app");
Appium Built-In Method : — “.removeApp(appId)”:
driver.removeApp(appId);
Description:
This method uninstalls the application from the Device.
It’s typically used when you need to perform a clean install of the app during your test, ensuring that no previous data or settings affect the current test session.
- Purpose: Uninstalls the app from the device.
- Effect: The app is completely removed, including its data and cache (unless specified otherwise).
- Use Case: Useful for testing installation workflows or resetting the device state.
Example (Appium):
//Uninstall the wdiodemo app
driver.removeApp("com.wdiodemoapp");
System.out.println("Uninstall the wdiodemo app");
Important: Difference Between methods .terminateApp() Vs .removeApp()

“Complete Code: Optimized and Ready to Use”:
package com.appiumguide.appiumconcepts;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
/**
* Script Details - Appium Concepts : Difference between methods .activateApp Vs .terminateApp Vs .removeApp
*
* appium-java-client version: Latest
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class DiffBTWMethods {
private AndroidDriver driver;
@BeforeTest
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixel8");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(100));
}
@Test
public void test() throws InterruptedException, IOException {
//Case-1: Start an wdiodemo app
driver.activateApp("com.wdiodemoapp");
System.out.println("Started wdiodemo app successfully");
Thread.sleep(5000);
//Case-2: Open the wdiodemo app from background
driver.activateApp("com.wdiodemoapp");
System.out.println("Opened the wdiodemo app successfully");
//Close the wdiodemo app
driver.terminateApp("com.wdiodemoapp");
System.out.println("Close the wdiodemo app");
Thread.sleep(10000);
//Uninstall the wdiodemo app
driver.removeApp("com.wdiodemoapp");
System.out.println("Uninstall the wdiodemo app");
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Completed");
}
}
}
GitHub Link:
No comments:
Post a Comment