2

in my uiautomator test, i want to do something like

if( wifi is connected) {
    doSth();
} else {
    doSthElse();
}

Can I check that?

Kevin Hsueh
  • 53
  • 1
  • 4

2 Answers2

1

Since version 21 (?) UiAutomator has access to Instrumentation and thus to Context.

Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
Context context = instrumentation.getContext();
WifiManager wManager = context.getSystemService(Context.WIFI_SERVICE); //this works for other services as well

Now you can use all WifiManager methods such as:

wManager.isWifiEnabled()
wManager.setWifiEnabled(boolean)  //turn wifi on and off
Inês
  • 882
  • 3
  • 13
  • 28
0

method-1

Goto Wifi page- check "Conncted" string below your ssid.

method-2

check out put of adb shell netcfg and grep for valid ipaddress for wifi interface,mostly- wlan0.

method-3 (but works on rooted phones)

check out put of adb shell wpa_cli ifname=wlan0 status, It gives wifi connected or not information.

All above methods you can code with uiautomator.

Rilwan
  • 2,251
  • 2
  • 19
  • 28