Home » Android » Android Instrumentation Test Offline Cases

Android Instrumentation Test Offline Cases

Posted by: admin June 15, 2020 Leave a comment


For my instrumentation tests I am using Robotium. Mostly I am able to test everything but offline cases.

As soon as I disable data (using adb, F8 shortcut in emulator, etc. …) the test disconnects. It goes on in the device/emulator but no results are reported.

So, I have got an idea to put just the app in offline mode and not the whole device. The problem is I don’t know how…

Using iptablesApi I would need to root my device. I have read that Mobiwol app uses some kind of a VPN to restrict apps internet access without the need of rooting a device.

How does Mobiwol app blocks the internet connection per application? Or is there another way how to test apks offline?

EDIT 12/30/2014

I forgot to say that I am able to run tests offline but I have to start tests when the device is in offline state. Currently, I divided my tests into OFFLINE and ONLINE ones. After running ONLINEs I execute the famous adb kill-server and adb start-server. After that I execute OFFLINEs.

How to&Answers:

Just making a few suggestions since there seem to be different questions here.
1) If all you want to do is turn off the data before running the OFFLINE test case you might want to simply try using robotium itself to do so..

For WiFi:

WifiManager wifi=(WifiManager)solo.getCurrentActivity().getSystemService(Context.WIFI_SERVICE);

For Mobile Data(using reflections):

ConnectivityManager dataManager=(ConnectivityManager)solo.getCurrentActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

Method dataClass = ConnectivityManager.class.getDeclaredMethod(“setMobileDataEnabled”, boolean.class);
dataClass.invoke(dataManager, true);

You can do the two above calls in the setup() method before running the individual test case in the OFFLINE suite.
Once all the test case in the OFFLINE suite are done with you can enable the WiFi/DATA back on in the teardown() method at the very end.

2) Looking at the app that you posted in the OP, it seems pretty much that it:

  • Uses the ipTables based on the OS version

  • Creates a script header based on the UID’s for all the applications
    that need WiFi/Data

  • Should be getting the list of installed apps on the device along with
    any hidden apps etc from the package manager.

  • And again executes scripts based on user selection for black list and
    overrides the existing rules in the ipTable with the user desired

Pretty sure though must have been quite hard to code all of that..Sounds much easier in the form of bullet points.

Hope this helps you somewhat.

P.S: If you do figure out something please post an updated answer, would like to know how did you make it work

Update: Make sure you have the neccessary permissions for setting the WiFi/Data on/off in your application manifest. NOT the test apk manifest. IT HAS TO BE THE APPLICATION MANIFEST ITSELF.
There is this library which might help you. Its an extension to solo. http://adventuresinqa.com/2014/02/17/extsolo-library-to-extend-your-robotium-test-automation/


There is great library from LinkedIn Test Butler, you can enable, disable both WiFi and mobile data by simply calling:


The main advantage of this library is that it does not require any permission in your manifest, for more details please refer to project website:



Sorry if I’m oversimplifying this, but what about just putting the phone/emulator in airplane mode? Through the actual user interface. That’s what I do to test offline cases.


After spending hours trying to do it similar to user2511882s solution, I still had an exception, because of missing permissions (yes the “modify system settings” permission was activated).

I ended up doing it with UI automator:

public static void setAirplaneMode(boolean enable)
    if ((enable ? 1 : 0) == Settings.System.getInt(getInstrumentation().getContext().getContentResolver(),
            Settings.Global.AIRPLANE_MODE_ON, 0))
    UiDevice device = UiDevice.getInstance(getInstrumentation());
    // Find the text of your language
    BySelector description = By.desc("Airplane mode");
    // Need to wait for the button, as the opening of quick settings is animated.
    device.wait(Until.hasObject(description), 500);
    getInstrumentation().getContext().sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

You will need the ACCESS_NETWORK_STATE in your androidTest manifest file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Don’t forget to disable it after the test.

If you have other languages than english, you need to change “Airplane mode” to the text of your language. As I have several translations, I read it from a ressource string.