注:Appium并不是所有的手机都能打开数据关闭数据流量成功,打开wifi有确认按钮,需要自己设置

下面放代码

# /usr/bin/env python
# -*- coding: utf-8 -*-
from appium.webdriver.connectiontype import ConnectionType
class internet_class():
    '''
        # WIFI
        ConnectionType.WIFI_ONLY

        # 数据流量
        ConnectionType.DATA_ONLY

        # 飞行模式
        ConnectionType.AIRPLANE_MODE

        # 无网络模式
        ConnectionType.NO_CONNECTION

        # 全部都打开
        ConnectionType.ALL_NETWORK_ON

    '''
    def __init__(self,driver):
        self.driver = driver

    def getwebstate(self):
        '''
        获取当前网络的状态
        :return:
        '''
        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)

    def wifi(self):
        '''
        仅wifi
        :return:
        '''
        # 设置 网络
        self.driver.set_network_connection(ConnectionType.WIFI_ONLY)
        print(self.getwebstate())
    def only(self):
        '''
        仅数据数据
        :return:
        '''
        self.driver.set_network_connection(ConnectionType.DATA_ONLY)
        print(self.getwebstate())

    def airplane_mode(self):
        '''
        飞行模式
        :return:
        '''
        self.driver.set_network_connection(ConnectionType.AIRPLANE_MODE)
        print(self.getwebstate())
    def all_networe_on(self):
        '''
        所有网络都打开
        :return:
        '''
        self.driver.set_network_connection(ConnectionType.ALL_NETWORK_ON)
        print(self.getwebstate())

作者公众号:jenny chat

微信公众号

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐