Appium Concepts: How to Fetch/Get “appPackage” and “appActivity” details during runtime(programmatically)
“.getCurrentPackage()” & “.currentActivity()”

🎥 Check Out the Video Below for Complete Details!
Introduction:
When working with Appium, it’s essential to know the appPackage and appActivity of an Android application to properly configure the Appium capabilities. While this information can be manually retrieved using ADB (Android Debug Bridge), in some cases, it is helpful to extract them dynamically during runtime.
Inthis article, we’ll explore how to retrieve appPackage and appActivity programmatically.
Why Do We Need appPackage and appActivity?
- appPackage is the unique identifier (package name) of the app.
- appActivity refers to the specific screen (activity) within the app that launches first when the application starts.
These values are required to launch the app correctly using Appium.
Appium client provides 2 methods:
To Fetch App Package — “.getCurrentPackage()”
The .getCurrentPackage() method in Appium is used to retrieve the package name of the currently running Android application.
Example:
driver.getCurrentPackage()
When to Use getCurrentPackage()?
- When you need to verify that the expected app is currently running.
- To debug app launches during automation.
- If you need to switch between different applications dynamically.
To Fetch App Acivity — “.currentActivity()”
The .currentActivity() method in Appium is used to get the name of the currently active activity in an Android application.
Example:
driver.currentActivity()
When to Use .currentActivity()
- To verify if the correct screen is opened after an action.
- To debug activity transitions in automation.
- Useful when handling deep links and app navigation.
“Complete Code: Optimized and Ready to Use”:
package com.appiumguide.appiumconcepts;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
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;
/**
* Script Details - Appium Concepts : How to Get "appPackage" and "appActivity" during runtime(programmatically)
*
* appium-java-client version: 9.3.0
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class PackActTest1 {
private AndroidDriver driver;
@BeforeTest
public void steup() throws MalformedURLException {
UiAutomator2Options cap =new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixal7a");
cap.setAppPackage("com.google.android.deskclock");
cap.setAppActivity("com.android.deskclock.DeskClock");
driver=new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Execution Completed");
}
}
@Test
public void PackActSample1() {
String pack=driver.getCurrentPackage();
System.out.println("Current apppackage details - "+pack);
String act=driver.currentActivity();
System.out.println("Current appActivity details - "+act);
}
}
Conclusion:
By implementing these methods, you can automate the process of retrieving appPackage and appActivity dynamically, making your test scripts more adaptable and robust
Explore on GitHub:
No comments:
Post a Comment