背景
关闭wifi,重新开启
方法一
from appium.webdriver.connectiontype import ConnectionType
info = {0: "NO_CONNECTION(没网络)",
1: "AIRPLANE_MODE(飞行模式)",
2: "WIFI_ONLY(仅wifi)",
4: "DATA_ONLY(仅数据)",
6: "ALL_NETWORK_ON(所有网络都打开)"}
state = self.driver.network_connection
return info.get(state)
# 获取当前网络
a = driver.network_connection
print('a:',a)
# 仅wifi
self.driver.set_network_connection(2)
切换网络01246就报错 可以尝试一下
方法二
原理分析
使用Appium Setting开启wifi or 关闭wifi,其实就是执行这个命令:
# 开启wifi
adb shell am broadcast -a io.appium.settings.wifi -n io.appium.settings/.receivers.WiFiConnectionSettingReceiver --es setstatus enable
# 关闭wifi
adb shell am broadcast -a io.appium.settings.wifi -n io.appium.settings/.receivers.WiFiConnectionSettingReceiver --es setstatus disable
但是通过broadcast 申请权限后,就会弹出系统弹窗,我们需要对弹窗内容做处理。
方法三
(1)思路1 - 手动允许Appium setting的wifi权限,可行✅
Appium setting 在一个设备上一般只安装一次,下次不需要重新安装。
也就是只需要手动操作一次,也算一劳永逸。
(2)思路2 - 通过 svc 开启/关闭wifi, 可行 ✅
adb shell svc wifi enable 启动wifi
adb shell svc wifi disable 关闭wifi
其他信息:
本命令在小米11/华为mate40上执行生效,但不知道是否会有厂商限制 ,有待确认。
本命令必须在driver启动之前执行,不然可能会导致目标app anr -> 出现概率99%,不知道是有个啥大病。
我这边是这样使用 有点奇怪但是好用!
# 关闭wifi
def get_stop(self):
os.system('adb shell am broadcast -a io.appium.settings.wifi -n io.appium.settings/.receivers.WiFiConnectionSettingReceiver --es setstatus enable')
time.sleep(2)
# b = driver.network_connection
# print('b:', b)
# 打开wifi
def get_start(self):
os.system('adb shell svc wifi disable')
time.sleep(2)
# c = driver.network_connection
# print('c:', c)
原文链接:https://blog.csdn.net/YUICUI/article/details/123778853