Android Debug Bridge (adb) is a command-line tool. It lets you interact with an Android device. The adb commands facilitate a variety of device actions, such as installing and debugging apps, changing battery level, turning on/off the wifi etc. It has three components:

  • A client, which sends commands. The client runs on your development machine. You can invoke a client from a command-line terminal by issuing an adb command.
  • A daemon (adbd), which runs commands on a device. The daemon runs as a background process on each device.
  • A server, which manages communication between the client and the daemon. The server runs as a background process on your development machine.

Today I’ll explain how to manipulate your mobile device’s battery level. As you all know, mobile testing is trivial. There are many components that change the state of the device. Those components are the battery, network and many more.

What happens after the battery is below %15? When your phone gets into power saver mode, network turns itself to off state to save your battery power. After you start using the app, IP address change. If there’s an IP restriction on your app, your session will be terminated. That’s a business rule in finance app’s in Turkey.

Generally, those situations are tested manually. So how can we implement those situations in our mobile automation testing process?

Prerequisite

You need to install Android SDK Platform-Tools package. Here is the website to install it.

https://developer.android.com/studio/releases/platform-tools

Battery Commands

dumpsys battery set level %s – It let you change the level from 0 to 100

dumpsys battery set status %s – It let you change the level to Unknown, Charging, Discharging, Not Charging or Full.

dumpsys battery reset – It let you reset the battery change made through adb.

dumpsys battery set usb %s – It let you change the status of USB connection. It can be ON or OFF.

Using those command via command line

Some command requires device Id, some does not. In order to get the unique device id, you should run “adb devices” command in command line.

Then you should run your command with below syntax.

So, if you want to change battery level to 5, you should run

Here’s the project that we just started.

A sample function from our BatteryCommand class

Process Executor class to execute ADB commands.

Sample test class to execute our commands.

Here’s the result of the execution

GitHub Project 

Thanks.
Canberk Akduygu