Simulate Turning Off Wi-Fi/Connection on Mobile Devices and Simulators

By Rafaela Souza
Thursday, 05 February, 2015


Enable/Disable Data Connections and Wi-Fi

Device/Simulator – Android:

For Android devices, you can easily create a portable Wi-Fi hotspot which can be controlled using ‘adb’ commands.  Turn on Airplane mode on the device and turn Wi-Fi back on.  This will prevent the 3G connection from providing data access.  You can then launch your application and disconnect your Wi-Fi router to simulate a loss of connectivity.

Ruby Methods to Enable/Disable Network:

The following two ruby methods can be defined within your test script and then called to simulate loss of connectivity:

def disable_network %x(adb shell svc wifi disable) end def enable_network %x(adb shell svc wifi enable) end

Command Line Steps to Enable/Disable Wi-Fi on Device:

This can be run from the command line or included within your test script and must be run prior to running your tests:

%x(adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings) (For selecting Next element on the screen) %x(adb shell input keyevent 20)

For selecting previous element on the screen

%x(adb shell input keyevent 19)

For clicking on the selected element

%x(adb shell input keyevent 23)

So you have to use select next or select previous key events and then you the click event to toggle the Wi-Fi:

Device/Simulator – IOS:

  • Set up a Wi-Fi router which can be used for testing
  • Turn on Airplane mode on the device
  • Turn Wi-Fi back on (this will prevent the 3G connection from providing data access)
  • Launch the application
  • Now disconnect your Wi-Fi router to simulate a loss of connectivity  

The only option available on iOS is to switch on Airplane mode and connect the device to the Wi-Fi.  When you want to disconnect Wi-Fi, switch off the router itself.  You can use any computer to create the Wi-Fi access point.

In summary, you have to connect your device to a Wi-Fi connection that can be controlled with System commands.  The problem is that you will lose the connection of your machine.

The following two ruby methods should be defined within your test script and can be called to simulate loss of connectivity:

def disable_network %x[ifconfig Wi-Fi down] %x[ifconfig en4 down] end def enable_network %x[ifconfig Wi-Fi up] %x[ifconfig en4 up] end

If you have an Android phone, you can easily create a portable Wi-Fi Hotspot which can be controlled using adb commands.

 
10135 views
  Comments  
* Comments subject to moderation