Module appium.webdriver.extensions.android.network

Expand source code
#!/usr/bin/env python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TypeVar

from selenium import webdriver

from appium.common.helper import extract_const_attributes
from appium.common.logger import logger
from appium.webdriver.mobilecommand import MobileCommand as Command

T = TypeVar('T', bound='Network')


class NetSpeed:
    GSM = 'gsm'  # GSM/CSD (up: 14.4(kbps), down: 14.4(kbps))
    SCSD = 'scsd'  # HSCSD (up: 14.4, down: 57.6)
    GPRS = 'gprs'  # GPRS (up: 28.8, down: 57.6)
    EDGE = 'edge'  # EDGE/EGPRS (up: 473.6, down: 473.6)
    UMTS = 'umts'  # UMTS/3G (up: 384.0, down: 384.0)
    HSDPA = 'hsdpa'  # HSDPA (up: 5760.0, down: 13,980.0)
    LTE = 'lte'  # LTE (up: 58,000, down: 173,000)
    EVDO = 'evdo'  # EVDO (up: 75,000, down: 280,000)
    FULL = 'full'  # No limit, the default (up: 0.0, down: 0.0)


class Network(webdriver.Remote):

    @property
    def network_connection(self) -> int:
        """Returns an integer bitmask specifying the network connection type.

        Android only.
        Possible values are available through the enumeration `appium.webdriver.ConnectionType`
        """
        return self.execute(Command.GET_NETWORK_CONNECTION, {})['value']

    def set_network_connection(self, connection_type: int) -> int:
        """Sets the network connection type. Android only.

        Possible values:
            Value (Alias)      | Data | Wifi | Airplane Mode
            -------------------------------------------------
            0 (None)           | 0    | 0    | 0
            1 (Airplane Mode)  | 0    | 0    | 1
            2 (Wifi only)      | 0    | 1    | 0
            4 (Data only)      | 1    | 0    | 0
            6 (All network on) | 1    | 1    | 0
        These are available through the enumeration `appium.webdriver.ConnectionType`

        Args:
            connection_type (int): a member of the enum appium.webdriver.ConnectionType

        Return:
            int: Set network connection type
        """
        data = {
            'parameters': {
                'type': connection_type
            }
        }
        return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']

    def toggle_wifi(self) -> T:
        """Toggle the wifi on the device, Android only.

        Returns:
            `appium.webdriver.webdriver.WebDriver`
        """
        self.execute(Command.TOGGLE_WIFI, {})
        return self

    def set_network_speed(self, speed_type: str) -> T:
        """Set the network speed emulation.

        Android Emulator only.

        Args:
            speed_type (str): The network speed type.
                A member of the const appium.webdriver.extensions.android.network.NetSpeed.

        Usage:
            self.driver.set_network_speed(NetSpeed.LTE)

        Returns:
            `appium.webdriver.webdriver.WebDriver`
        """
        constants = extract_const_attributes(NetSpeed)
        if speed_type not in constants.values():
            logger.warning(
                f'{speed_type} is unknown. Consider using one of {list(constants.keys())} constants. (e.g. {NetSpeed.__name__}.LTE)')

        self.execute(Command.SET_NETWORK_SPEED, {'netspeed': speed_type})
        return self

    # pylint: disable=protected-access

    def _addCommands(self) -> None:
        self.command_executor._commands[Command.TOGGLE_WIFI] = \
            ('POST', '/session/$sessionId/appium/device/toggle_wifi')
        self.command_executor._commands[Command.GET_NETWORK_CONNECTION] = \
            ('GET', '/session/$sessionId/network_connection')
        self.command_executor._commands[Command.SET_NETWORK_CONNECTION] = \
            ('POST', '/session/$sessionId/network_connection')
        self.command_executor._commands[Command.SET_NETWORK_SPEED] = \
            ('POST', '/session/$sessionId/appium/device/network_speed')

Classes

class NetSpeed (*args, **kwargs)
Expand source code
class NetSpeed:
    GSM = 'gsm'  # GSM/CSD (up: 14.4(kbps), down: 14.4(kbps))
    SCSD = 'scsd'  # HSCSD (up: 14.4, down: 57.6)
    GPRS = 'gprs'  # GPRS (up: 28.8, down: 57.6)
    EDGE = 'edge'  # EDGE/EGPRS (up: 473.6, down: 473.6)
    UMTS = 'umts'  # UMTS/3G (up: 384.0, down: 384.0)
    HSDPA = 'hsdpa'  # HSDPA (up: 5760.0, down: 13,980.0)
    LTE = 'lte'  # LTE (up: 58,000, down: 173,000)
    EVDO = 'evdo'  # EVDO (up: 75,000, down: 280,000)
    FULL = 'full'  # No limit, the default (up: 0.0, down: 0.0)

Class variables

var EDGE

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var EVDO

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var FULL

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var GPRS

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var GSM

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var HSDPA

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var LTE

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var SCSD

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var UMTS

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

class Network (command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False, file_detector=None, options=None)

Controls a browser by sending commands to a remote server. This server is expected to be running the WebDriver wire protocol as defined at https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol

:Attributes: - session_id - String ID of the browser session started and controlled by this WebDriver. - capabilities - Dictionaty of effective capabilities of this browser session as returned by the remote server. See https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities - command_executor - remote_connection.RemoteConnection object used to execute commands. - error_handler - errorhandler.ErrorHandler object used to handle errors.

Create a new driver that will issue commands using the wire protocol.

:Args: - command_executor - Either a string representing URL of the remote server or a custom remote_connection.RemoteConnection object. Defaults to 'http://127.0.0.1:4444/wd/hub'. - desired_capabilities - A dictionary of capabilities to request when starting the browser session. Required parameter. - browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested. Optional. - proxy - A selenium.webdriver.common.proxy.Proxy object. The browser session will be started with given proxy settings, if possible. Optional. - keep_alive - Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive. Defaults to False. - file_detector - Pass custom file detector object during instantiation. If None, then default LocalFileDetector() will be used. - options - instance of a driver options.Options class

Expand source code
class Network(webdriver.Remote):

    @property
    def network_connection(self) -> int:
        """Returns an integer bitmask specifying the network connection type.

        Android only.
        Possible values are available through the enumeration `appium.webdriver.ConnectionType`
        """
        return self.execute(Command.GET_NETWORK_CONNECTION, {})['value']

    def set_network_connection(self, connection_type: int) -> int:
        """Sets the network connection type. Android only.

        Possible values:
            Value (Alias)      | Data | Wifi | Airplane Mode
            -------------------------------------------------
            0 (None)           | 0    | 0    | 0
            1 (Airplane Mode)  | 0    | 0    | 1
            2 (Wifi only)      | 0    | 1    | 0
            4 (Data only)      | 1    | 0    | 0
            6 (All network on) | 1    | 1    | 0
        These are available through the enumeration `appium.webdriver.ConnectionType`

        Args:
            connection_type (int): a member of the enum appium.webdriver.ConnectionType

        Return:
            int: Set network connection type
        """
        data = {
            'parameters': {
                'type': connection_type
            }
        }
        return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']

    def toggle_wifi(self) -> T:
        """Toggle the wifi on the device, Android only.

        Returns:
            `appium.webdriver.webdriver.WebDriver`
        """
        self.execute(Command.TOGGLE_WIFI, {})
        return self

    def set_network_speed(self, speed_type: str) -> T:
        """Set the network speed emulation.

        Android Emulator only.

        Args:
            speed_type (str): The network speed type.
                A member of the const appium.webdriver.extensions.android.network.NetSpeed.

        Usage:
            self.driver.set_network_speed(NetSpeed.LTE)

        Returns:
            `appium.webdriver.webdriver.WebDriver`
        """
        constants = extract_const_attributes(NetSpeed)
        if speed_type not in constants.values():
            logger.warning(
                f'{speed_type} is unknown. Consider using one of {list(constants.keys())} constants. (e.g. {NetSpeed.__name__}.LTE)')

        self.execute(Command.SET_NETWORK_SPEED, {'netspeed': speed_type})
        return self

    # pylint: disable=protected-access

    def _addCommands(self) -> None:
        self.command_executor._commands[Command.TOGGLE_WIFI] = \
            ('POST', '/session/$sessionId/appium/device/toggle_wifi')
        self.command_executor._commands[Command.GET_NETWORK_CONNECTION] = \
            ('GET', '/session/$sessionId/network_connection')
        self.command_executor._commands[Command.SET_NETWORK_CONNECTION] = \
            ('POST', '/session/$sessionId/network_connection')
        self.command_executor._commands[Command.SET_NETWORK_SPEED] = \
            ('POST', '/session/$sessionId/appium/device/network_speed')

Ancestors

  • selenium.webdriver.remote.webdriver.WebDriver

Subclasses

Instance variables

var network_connection

Returns an integer bitmask specifying the network connection type.

Android only. Possible values are available through the enumeration appium.webdriver.ConnectionType

Expand source code
@property
def network_connection(self) -> int:
    """Returns an integer bitmask specifying the network connection type.

    Android only.
    Possible values are available through the enumeration `appium.webdriver.ConnectionType`
    """
    return self.execute(Command.GET_NETWORK_CONNECTION, {})['value']

Methods

def set_network_connection(self, connection_type)

Sets the network connection type. Android only.

Possible values: Value (Alias) | Data | Wifi | Airplane Mode ------------------------------------------------- 0 (None) | 0 | 0 | 0 1 (Airplane Mode) | 0 | 0 | 1 2 (Wifi only) | 0 | 1 | 0 4 (Data only) | 1 | 0 | 0 6 (All network on) | 1 | 1 | 0 These are available through the enumeration appium.webdriver.ConnectionType

Args

connection_type : int
a member of the enum appium.webdriver.ConnectionType

Return

int
Set network connection type
Expand source code
def set_network_connection(self, connection_type: int) -> int:
    """Sets the network connection type. Android only.

    Possible values:
        Value (Alias)      | Data | Wifi | Airplane Mode
        -------------------------------------------------
        0 (None)           | 0    | 0    | 0
        1 (Airplane Mode)  | 0    | 0    | 1
        2 (Wifi only)      | 0    | 1    | 0
        4 (Data only)      | 1    | 0    | 0
        6 (All network on) | 1    | 1    | 0
    These are available through the enumeration `appium.webdriver.ConnectionType`

    Args:
        connection_type (int): a member of the enum appium.webdriver.ConnectionType

    Return:
        int: Set network connection type
    """
    data = {
        'parameters': {
            'type': connection_type
        }
    }
    return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']
def set_network_speed(self, speed_type)

Set the network speed emulation.

Android Emulator only.

Args

speed_type : str
The network speed type. A member of the const appium.webdriver.extensions.android.network.NetSpeed.

Usage

self.driver.set_network_speed(NetSpeed.LTE)

Returns

WebDriver

Expand source code
def set_network_speed(self, speed_type: str) -> T:
    """Set the network speed emulation.

    Android Emulator only.

    Args:
        speed_type (str): The network speed type.
            A member of the const appium.webdriver.extensions.android.network.NetSpeed.

    Usage:
        self.driver.set_network_speed(NetSpeed.LTE)

    Returns:
        `appium.webdriver.webdriver.WebDriver`
    """
    constants = extract_const_attributes(NetSpeed)
    if speed_type not in constants.values():
        logger.warning(
            f'{speed_type} is unknown. Consider using one of {list(constants.keys())} constants. (e.g. {NetSpeed.__name__}.LTE)')

    self.execute(Command.SET_NETWORK_SPEED, {'netspeed': speed_type})
    return self
def toggle_wifi(self)

Toggle the wifi on the device, Android only.

Returns

WebDriver

Expand source code
def toggle_wifi(self) -> T:
    """Toggle the wifi on the device, Android only.

    Returns:
        `appium.webdriver.webdriver.WebDriver`
    """
    self.execute(Command.TOGGLE_WIFI, {})
    return self