How to Get Battery Information using Appium | Mobile Automation - Battery Charging Level Information
In this post will learn how to get battery information from mobile using
Appium.
While doing automation, many times we come across testing mobile app
behavior based on the Battery status like how app is behaving when battery
is very LOW and when battery is FULL.
We can automate this functionality using executeScript by
sending “mobile:batteryInfo” command.
Example:
driver.executeScript("mobile:batteryInfo");
Before writing code, let’s learn what would be return values of this
“mobile:batteryInfo” command.
When we execute this command, we will get return values below:
{level=1, state=4}
Where state value describes the charging state of the mobile:
Where value -
1 : means UNKNOWN
2 : means CHARGING
3 : means DISCHARGING
4 : means NOT_CHARGING
5 : means FULL
where Level value describes the batter charged level - Battery level
in range [0.0, 1.0], where 1.0 means 100% charge. This returns in
double.
Example : if battery charged 81%, then we get Level value as 0.81
Now lets write one sample program and lets see how we can get the battery
information:
Program:
package tests;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
public class BaseTest
{
public static AppiumDriver<MobileElement> driver;
public static void main(String args[]) throws MalformedURLException, InterruptedException
{
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability("deviceName", "emulator-5554"); //your device name from adb devices
cap.setCapability("udid", "emulator-5554");//your udid from adb devices
cap.setCapability("appActivity", "com.android.calculator2.Calculator");
cap.setCapability("appPackage", "com.google.android.calculator");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "R"); //your device platform version
driver=new AppiumDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Thread.sleep(5000);
System.out.println( driver.executeScript("mobile:batteryInfo"));
}
}
In this example I’m using emulator here:
Example 1:
Now I’m setting my emulator battery settings as below in the emulator (you
can see in the below screenshot) and checking the status by executing the
above code:
Battery Status: Not Charging
Charging Level : 100%
OUPUT:
Example 1 Output explanation:
Since my battery is not charging, I’m getting state =
4 (NOT_CHARGING)
And my battery charge is 100%, I’m getting level = 1 (100%
Charged)
Example 2:
Now I’m setting my emulator battery settings as below in the emulator (you
can see in the below screenshot) and checking the status by executing the
above code:
Battery Status: Charging
Charging Level : 67%
OUPUT:
Example 2 Output explanation:
Since my battery is Charging, I’m
getting state = 2 (CHARGING)
And my battery charge is 67%, I’m getting level = 0.67 (67% Charged)
Example 3:
Now I’m setting my emulator battery settings as below in the emulator (you
can see in the below screenshot) and checking the status by executing the
above code:
Battery Status: Discharging
Charging Level : 83%
OUPUT:
Example 3 Output explanation:
Since my battery is discharging, I’m getting state =
3 (DISCHARGING)
And my battery charge is 83%, I’m getting level = 0.83 (83% Charged)
Example 4:
Now I’m setting my emulator battery settings as below in the emulator (you
can see in the below screenshot) and checking the status by executing the
above code:
Battery Status: Full
Charging Level : 100%
OUPUT:
Example 4 Output explanation:
Since my battery is full, I’m getting state =
5 (FULL)
And my battery charge is 100%, I’m getting level = 1 (100% Charged)
Example 5:
Now I’m setting my emulator battery settings as below in the emulator (you
can see in the below screenshot) and checking the status by executing the
above code:
Battery Status: Unknown
Charging Level : 100%
OUPUT:
Example 5 Output explanation:
Since my battery status is unknown, I’m getting state =
1 (UNKNOWN)
And my battery charge is 100%, I’m getting level = 1 (100% Charged)
if you encounter any issues please let me know in the comments section below.
No comments:
Post a Comment
If any suggestions or issue, please provide