Simulate turn off wifi/connection on devices and simulators

Simulate disable network


Device/Simulator – Android:

%x(adb shell am start -a android.intent.action.MAIN -n

(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 toggel the wifi.

Simulator – Android:

def disable_network
%x(adb shell svc wifi disable)

def enable_network
%x(adb shell svc wifi enable)

Device/Simulator – IOS:

First, setup a WiFi router that you can use for testing.

You can turn on Airplane mode on the phone, then turn WiFi back on. This will prevent the 3G connection from providing data access.

Launch your application. Now you can simply disconnect your WiFi router to simulate a loss of connectivity.

So the only option available is switch on airplane mode on phone and connect it to the wifi. Once you want to disconnect wifi switch off the wifi router itself. You can use any laptop to create the testing wifi access point.

Summarizing you have to connect your device in a wifi connection that you canΒ control with System commands, the problem is that you will lost the connection of your machine.

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

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

But if you have an android phone you can easily create a portable wifi Hotspot which can be controlled using adb commands.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.