Android Robotium : Make Wi-Fi and Mobile Data Network Enable & Disable Programatically

android

While working with android application development or automating test scripts for android application testing sometimes we need to ON/OFF Wi-Fi and Mobile Data Access feature explicitly.

Android library facilitates to do that in the following way:

For Wi-Fi:

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

Passing false will disable Wi-Fi and true enables it.

For Mobile Data:

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

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

Please Note:

The following permission must be configured in AndroidManifest.xml file .
For Wi-Fi:

android.permission.ACCESS_WIFI_STATE

android.permission.CHANGE_WIFI_STATE

For Mobile Data:

android.permission.CHANGE_NETWORK_STATE

2 thoughts on “Android Robotium : Make Wi-Fi and Mobile Data Network Enable & Disable Programatically

Leave a comment