Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Instrumentation Test Offline Cases

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.

Question 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.

like image 351
Amio.io Avatar asked Dec 23 '14 13:12

Amio.io


People also ask

What are instrumentation tests in Android?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.

How do you write test cases for Android Apps?

Test cases should be written in such a way that they allow a person to test only one feature at a time. One should not overlap or complicate test cases. Cover all the positive and negative probabilities of the test outcomes. Write in simple language with exact and accurate names of forms, fields, etc.


1 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..

Example: For WiFi:

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

For Mobile Data(using reflections):

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

Method dataClass = ConnectivityManager.class.getDeclaredMethod(“setMobileDataEnabled”, boolean.class);
dataClass.setAccessible(true);
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 rules.

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/

like image 142
user2511882 Avatar answered Nov 07 '22 08:11

user2511882