appium關於當前網絡情況測試,實現打開關閉網絡(不需要root測試機)
# python
from appium.webdriver.connectiontype import ConnectionType
#設置斷網,調用ConnectionType的狀態
self.driver.set_network_connection(ConnectionType.NO_CONNECTION)
# 打印當前狀態,返回值是0,1,2,4,6
print(self.driver.network_connection)
輸出:
0
可以查看ConnectionType封裝的幾種可選類型,
class ConnectionType(object):
NO_CONNECTION = 0
AIRPLANE_MODE = 1
WIFI_ONLY = 2
DATA_ONLY = 4
ALL_NETWORK_ON = 6
可以針對當前網絡狀態封裝
def getwebstate(self):
info={0:"NO_CONNECTION",
1:"AIRPLANE_MODE",
2:"WIFI_ONLY",
4:"DATA_ONLY",
6:"ALL_NETWORK_ON"}
state=self.driver.network_connection
return info.get(state)
如上:
self.driver.set_network_connection(ConnectionType.WIFI_ONLY)
print(getwebstate())
輸出:WIFI_ONLY
不再直接輸出對應2
