最新公司上線的app,開發(fā)童鞋從后臺看到android版本會有一定的概率在操作app時更耻,網(wǎng)絡(luò)斷開再連接的時候會出現(xiàn)閃退問題测垛。開發(fā)針對問題修改了下代碼,需要我們再驗證下秧均,但通過手機(jī)的通知欄關(guān)閉和打開Wi-Fi再操作app食侮,操作特別繁瑣。于是就在網(wǎng)上找到了控制wifi開關(guān)的shell語句目胡,利用python編寫了一個簡單的控制wifi開關(guān)的腳本(因為是菜鳥锯七,所以只能保證腳本能正常運行),下面就是代碼啦誉己,僅以此篇記錄下眉尸。
1、首先從網(wǎng)上找到關(guān)于開啟Wi-Fi的語句:adb shell svc wifi enable 關(guān)閉wifi:adb shell svc disable
2巨双、所有寫個循環(huán)方法噪猾,在腳本中不斷調(diào)用這兩個shell命令即可實現(xiàn):在操作app的時候,手機(jī)會不斷的自動斷網(wǎng)筑累,再自動重連Wi-Fi袱蜡,而不再需要手動去操作wifi啦
import os
import time
class App():
def __init__(self,count):
self.count = count
# 開啟wifi的方法
def openWifi(self):
cmd = 'adb shell svc wifi enable'
os.popen(cmd)
time.sleep(60)
# 關(guān)閉wifi的方法
def closeWifi(self):
cmd = 'adb shell svc wifi disable'
time.sleep(5)
#控制wifi循環(huán)的方法
def controlWifi(self):
i = 1
while (self.count >0):
print("第 %d 次執(zhí)行開關(guān)Wi-Fi操作" % i)
self.closeWifi()
self.openWifi()
i = i +1
self.count = self.count - 1
if __name__ == '__main__':
#控制Wi-Fi開關(guān)執(zhí)行100次
app = App(100)
app.controlWifi()