How to control ADB commands on Android using code JAVA + Appium

How to control ADB commands on Android using code JAVA + Appium

Hey Guys

After many searches, the best solution I found to work with ADB commands on real devices

How to control ADB commands on Android using code JAVA + Appium

(Those who use emulators for testing are less relevant because there you can use commands from the Appium library)

The first thing is to write a method that sends a command to the CommandLine

    public static void runShellScript(String command) {


        int iExitValue;
        String sCommandString;
        sCommandString = command;
        CommandLine oCmdLine = CommandLine.parse(sCommandString);
        DefaultExecutor oDefaultExecutor = new DefaultExecutor();
        oDefaultExecutor.setExitValue(0);


        try{
            iExitValue = oDefaultExecutor.execute(oCmdLine);


        } catch (ExecuteException e){
            System.out.println("Fail");
            e.printStackTrace();
        } catch (IOException e){
            System.out.println("Denied");
            e.printStackTrace();
        }

    }        

Then implement the method like this


runShellScript("adb shell svc data enable");        

and use anywhere as needed in tests :)

For example:

If we want to test a certain application whose operating requirements require active Bluetooth + WIFI + Data + Location

So we will send the following commands:


runShellScript("adb shell svc data enable")
runShellScript("adb shell svc wifi enable");
runShellScript("adb shell am broadcast -a io.appium.settings.bluetooth --es setstatus enable");
runShellScript("adb shell settings put secure location_mode 3");;        

It is important to note that the commands vary according to the type of version and device, so you must first check what the correct password is.

  • Enable bluetooth: adb shell am broadcast -a io.appium.settings.bluetooth --es setstatus enable
  • Disable bluetooth: adb shell am broadcast -a io.appium.settings.bluetooth --es setstatus disable
  • Enable location: adb shell settings put secure location_mode 3
  • Disable location: adb shell settings put secure location_mode 0
  • Enable mobile data: adb shell svc data enable
  • Disable mobile data: adb shell svc data disable
  • Enable wifi: adb shell svc wifi enable
  • Disable wifi: adb shell svc wifi disable

*You can find all other commands online*

#automation #seleniumwebdriver #seleniumtesting #appium #adb #android

Enjoy :)

תודה רבה לך על השיתוף🙂 אני מזמין אותך לקבוצה שלי: הקבוצה מחברת בין ישראלים במגוון תחומים, הקבוצה מייצרת לקוחות,שיתופי פעולה ואירועים. https://chat.whatsapp.com/IyTWnwphyc8AZAcawRTUhR

Like
Reply

To view or add a comment, sign in

More articles by Elad Tuviana

Others also viewed

Explore content categories