Checking the Battery Level on Android Devices

Applies to TestComplete 14.72, last modified on April 22, 2021

You can check the battery level of your Android device level in your tests to make sure your Android application does not consume more energy than it should. For example, you can measure the battery level at the beginning and at the end of a test and see if the difference is acceptable.

To get the battery level, you can use the device’s DeviceInfo.BatteryLevel property. It returns the battery level as a percentage. Keep in mind that different devices consume energy differently.

Preventing the Device From Charging

To test Android applications, you need to connect your Android device to the computer via a USB cable. However, connected devices are charged. To test the battery usage, you need to prevent the device from charging. Android and Windows do not allow you to disable charging, but you can try doing the following:

  • Disable USB power supply in the BIOS. Find the USB Voltage option under Chip Configuration. Not all motherboards have this option.

  • (Root access needed.) Disable USB charging through the Android terminal. The command line depends on the device manufacturer.

  • Use an unpowered USB hub.

  • Cut the red (power) wire in the USB cable used to connect the device to the computer.

Getting the Battery Level in Keyword Tests

In keyword tests, use the Call Object Method operation to get the device’s BatteryLevel property. To configure this operation:

  • Add the Call Object Method operation to your keyword test. TestComplete will display the Operation Parameters wizard.

  • Enter one of the following as the object name:

    • Aliases.device.DeviceInfo - if you use Name Mapping

    • Mobile.Device("MyDevice").DeviceInfo - if you do not use Name Mapping.

    • Mobile.Device("DeviceName").Desktop - if you do not use Name Mapping and the device is not the current one.

    Click Next.

  • Select BatteryLevel [Get] from the property list and click Finish.

Right after the Call Object Method operation, add an operation that will use the battery level value. For example, the Log Message operation to post the Last Operation Result value to the test log, or the Set Variable Value operation to save the value to a variable for later use.

Getting mobile device's battery level in keyword tests

Getting the Battery Level in Scripts

In scripts, you can get the device’s DeviceInfo.BatteryLevel value as shown in this example:

JavaScript, JScript

function Test()
{
  Mobile.SetCurrent("MyDevice")

  // Check the device's battery level
  var batteryLvl = Mobile.Device().DeviceInfo.BatteryLevel;
  Log.Message(batteryLvl);

  // Do something
  // ...


  // Check the battery level again
  batteryLvl = Mobile.Device().DeviceInfo.BatteryLevel;
  Log.Message(batteryLvl);
}

Python

def Test():
  Mobile.SetCurrent("MyDevice")

  # Check the device's battery level
  batteryLvl = Mobile.Device().DeviceInfo.BatteryLevel;
  Log.Message(batteryLvl);

  # Do something
  # ...

  # Check the battery level again
  batteryLvl = Mobile.Device().DeviceInfo.BatteryLevel;
  Log.Message(batteryLvl);

VBScript

Sub Test
  Dim batteryLvl
  Call Mobile.SetCurrent("MyDevice")

  ' Check the device's battery level
  batteryLvl = Mobile.Device.DeviceInfo.BatteryLevel
  Log.Message(batteryLvl)

  ' Do something
  ' ...


  ' Check the battery level again
  batteryLvl = Mobile.Device.DeviceInfo.BatteryLevel
  Log.Message(batteryLvl)
End Sub

DelphiScript

procedure Test;
var batteryLvl;
begin
  Mobile.SetCurrent('MyDevice');

  // Check the device's battery level
  batteryLvl := Mobile.Device.DeviceInfo.BatteryLevel;
  Log.Message(batteryLvl);

  // Do something
  // ...


  // Check the battery level again
  batteryLvl := Mobile.Device.DeviceInfo.BatteryLevel;
  Log.Message(batteryLvl);
end;

C++Script, C#Script

function Test()
{
  Mobile["SetCurrent"]("MyDevice")

  // Check the device's battery level
  var batteryLvl = Mobile["Device"]["DeviceInfo"]["BatteryLevel"];
  Log["Message"](batteryLvl);

  // Do something
  // ...


  // Check the battery level again
  batteryLvl = Mobile["Device"]["DeviceInfo"]["BatteryLevel"];
  Log["Message"](batteryLvl);
}

See Also

AndroidDeviceInfo Object

Highlight search results