Search code examples
androidrobotium

can wifi be switched on/off in test case through robotium


can we switch on/off the wi-fi of the device in a test cases in robotium? because i am testing an issue which needs wifi to be on in initial phase then turning off the wi-fi and continue with testing.


Solution

  • Yes you can do it, see the example:

    public void testNoNetworkConnection() throws Exception {

        setWifiEnabled(false);
    
        // do stuff solo.something
    
       setWifiEnabled(true);
    
    }
    
    private void setWifiEnabled(boolean state) {
        WifiManager wifiManager = (WifiManager)solo.getCurrentActivity().getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(state);
    }
    

    Remember to add permission in your Manifest file:

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

    EDIT: With the new Robotium 5.3.1 you can use setWiFiData(Boolean turnedOn) to turn wifi on or off (See documentation)

    Enjoy