Appium Concepts: How to check & update “Battery Level” & “Charging State” Programmatically
.getBatteryInfo().getLevel() & .getBatteryInfo().getState()

🎥 Check Out the Video Below for Complete Details!
Introduction
Inmobile automation testing, monitoring device battery status can be crucial — especially for battery-sensitive applications. Appium provides a handy method called driver.getBatteryInfo()
that allows testers to retrieve battery details from the connected mobile device during testing.
This method is particularly useful for Android automation, as it helps ensure that tests are run under consistent conditions or to simulate/test behavior based on battery level.
Syntax:
BatteryInfo batteryInfo = driver.getBatteryInfo();
Note: This method is available in Appium’s Java client (v7.3.0+). Equivalent methods exist for other language bindings as well.

Overview on batteryInfo.getLevel() method
This expression retrieves the current battery level of the device in use during an Appium test session.
double level = driver.getBatteryInfo().getLevel();

Overview on batteryInfo.getState() method
The “batteryInfo.getState()"
method returns the charging status of the mobile device at the time the battery info was retrieved.
BatteryState state = batteryInfo.getState();
Return Type: It returns an enum value of type ‘BatteryState'
.
Note: Value can be 1 to 5 which relates to status/State
1 — unknown,
2 — charging
3 — discharging
4 — not charging
5 — fully charged
Example:
In the below example, we have update the battery level to 100 & battery status (i.e. state to 3 — discharging) during runtime.

Output:

If we observe the output
- The Battery level 1.0 (means 100%)
- Battery Status/State is Discharging i.e. value-3
Practical Use Cases
- Battery-Dependent Features: Some apps may disable features like high-quality video streaming under low battery. You can use this method to simulate and verify such conditions.
- Logging Battery Usage: For long-running test sessions, tracking the battery level can help understand performance bottlenecks or excessive power usage.
- Condition-based Testing: Write conditional logic in your tests to run certain cases only under specific battery conditions.
“Complete Code: Optimized and Ready to Use”:
package demo;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
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;
import java.util.Scanner;
/**
* Script Details - Appium Concepts | How to check & update “Battery Level” & “Charging State” Programmatically
*
* appium-java-client version: 9.3.0
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class BatteryInfoDemo {
private AndroidDriver driver;
@BeforeTest
public void setup() throws IOException{
UiAutomator2Options capabilities=new UiAutomator2Options();
capabilities.setPlatformName("android");
capabilities.setAutomationName("uiautomator2");
capabilities.setDeviceName("Pixel8Pro");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), capabilities);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@Test
public void test1() throws IOException, InterruptedException {
Scanner scan =new Scanner(System.in);
for(int i=1;i<=5;i++) {
System.out.println("----------------------starting---------------------------------------");
System.out.println("Battery level before update -"+driver.getBatteryInfo().getLevel());
System.out.println("Battery state before update -"+driver.getBatteryInfo().getState());
System.out.println("------------------------------------------------------------");
System.out.println("please enter battery level");
String adb_level=scan.nextLine();
System.out.println("please enter battery state");
String adb_state=scan.nextLine();
System.out.println("------------------------------------------------------------");
Runtime.getRuntime().exec("adb shell dumpsys battery set level " +adb_level);
Runtime.getRuntime().exec("adb shell dumpsys battery set status "+adb_state);
Thread.sleep(5000);
System.out.println("------------------------------------------------------------");
System.out.println("Battery level after updating -"+driver.getBatteryInfo().getLevel());
System.out.println("Battery state after updating -"+driver.getBatteryInfo().getState());
}
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Execution Completed");
}
}
}
Limitations
- Platform Support: Battery info retrieval is fully supported on Android. iOS support might be limited due to OS restrictions.
- Permissions: On some Android versions, special permissions or settings may be required to fetch battery info.
- Appium Version: Ensure your Appium client and server versions are up-to-date to avoid compatibility issues.
Conclusion
"driver.getBatteryInfo()"
is a useful addition to the Appium toolkit for mobile automation testers. It allows testers to build more robust and battery-aware test scenarios, ensuring better reliability and real-world accuracy in mobile app testing.
If you’re automating Android apps and battery state matters for your testing scenarios, integrating this method can make your tests much smarter.
Explore on GitHub:
No comments:
Post a Comment