4

I am currently working on an Espresso test suite that covers offline functionality. In order for me to implement these tests, I need to create a method that I can call to toggle on/off network connectivity. So far, I have been able to toggle WiFi, but I have not been able to figure out how to turn off cellular data.

Any information is greatly appreciated.

Dillon C
  • 115
  • 10
  • Did you ever figure it out? – Shaishav Feb 11 '19 at 11:42
  • @Shaishav this worked for me: https://sqa.stackexchange.com/questions/23646/how-can-i-switch-on-off-airplane-mode-and-wifi-using-appium?answertab=votes#tab-top – Dillon C May 24 '19 at 17:47
  • Is any of this related to Coded UI which is part of Microsoft's Visual Studio? If not then please remove the misleading tag(s). – AdrianHHH May 26 '19 at 12:48
  • @DillonC how did you do it using espresso? I know you said it worked for you but that link that you shared is for Appium – Ilya G Apr 02 '20 at 18:15

3 Answers3

1

This solution worked for me: https://sqa.stackexchange.com/questions/23646/how-can-i-switch-on-off-airplane-mode-and-wifi-using-appium?answertab=votes#tab-top

You can also perform a really jank workaround. Note: I did this mainly for fun, do not promote the actual use of it ;)

#!/bin/bash

### SET Airplane Mode ON ###
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS \
&& for i in {1..5}
do 
    adb shell input keyevent 20
done \
&& adb shell input keyevent 23 \
&& adb shell input keyevent 4;

### Run tests ###

### SET Airplane Mode OFF ###
# NOTE: When Airplane Mode is enabled in API 28, "Mobile network" is disabled. Additionally, since Android Setting's Network & internet 
# is still running in the background, you'll have to select the down action two less times. 
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS \
&& for i in {1..3}
do 
    adb shell input keyevent 20
done \
&& adb shell input keyevent 23 \
&& adb shell input keyevent 4;
Dillon C
  • 115
  • 10
0

Try this solution from @zzz

(please upvote their answer on https://stackoverflow.com/a/53414845/191761)

public static void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
        connectivityManagerField.setAccessible(true);
        final Object connectivityManager = connectivityManagerField.get(conman);
        final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);

        setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}
Adam Burley
  • 5,551
  • 4
  • 51
  • 72
0

Another idea from @Illyct which can turn off both WiFi and mobile data via the shell:

InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc wifi disable")
InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc data disable")

I tested it and it's working for me.

Please upvote their answer at https://stackoverflow.com/a/64765567/191761 if you found it useful

Adam Burley
  • 5,551
  • 4
  • 51
  • 72