Run ADB Commands in Java Code for Device Manipulation

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

By |2018-11-28T01:06:09+00:00July 30th, 2018|Appium Tutorials|5 Comments

About the Author:

Canberk Akduygu is as Test Architect working on Test Automation and Performance Testing.

5 Comments

  1. Siddharth August 1, 2018 at 5:48 am - Reply

    public class BatteryCommand extends Command

    What should be mentioned Command base class? By importing import org.openqa.selenium.remote.Command;
    Its asking to add constructor
    public BatteryCommands(SessionId sessionId, String name) {
    super(sessionId, name);
    // TODO Auto-generated constructor stub
    }

    Could you please post complete end to end code ?

  2. Siddharth August 1, 2018 at 10:19 am - Reply

    Hey Onur,

    You always come in rescue.

    Thanks,

  3. Eyal August 6, 2018 at 1:48 pm - Reply

    Hi,
    If I want to get the adb shell command and get the output as a string,
    can you explain how to ?

    Thanks,
    Eyal

  4. Canberk Akduygu August 6, 2018 at 5:10 pm - Reply

    In exec function. i have this functionality. Create an InputStreamReader using Process class.
    Just take a look this code.

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.