Appium python client 网络连接相关 api 用法

来源:互联网 发布:洞主手工皂淘宝在哪 编辑:程序博客网 时间:2024/06/06 16:34

1.相关链接:http://blog.csdn.net/QingLang0213/article/details/48972403

原文 :https://testerhome.com/topics/3428

前言

今天有人问到我这个问题,并且尝试过传入数字/字符串作为参数均不成功。于是看了下,发现要找到正确用法需要对 client 源码有一定了解,并且 google 和 github 上都没有找到对应的使用示例,所以在这里记录一下。

问题描述

appium 一直有一个设置和获取当前各项连接状态的 api ,官方文档中可用参数及使用方法如下:

Value (Alias)DataWifiAirplane Mode0 (None)0001 (Airplane Mode)0012 (Wifi only)0104 (Data only)1006 (All network on)110
// javascript// set airplane modedriver.setNetworkConnection(1)// set wifi onlydriver.setNetworkConnection(2)// set data onlydriver.setNetworkConnection(4)// set wifi and datadriver.setNetworkConnection(6)

但在 Python client 中使用这样的代码会报错:

self.driver.set_network_connection(1)

报错信息:

ErrorTraceback (most recent call last):  File "/Users/NextGen/Documents/Framework/test.py", line 35, in test_search    self.driver.set_network_connection(1)  File "/Library/Python/2.7/site-packages/appium/webdriver/webdriver.py", line 623, in set_network_connection    'type': connectionType.valueAttributeError: 'int' object has no attribute 'value'

解决过程

参考资料:8.13. enum — Support for enumerations

首先,出现 AttributeError 这个很奇怪,这里传入的东西为啥会有 value 这个属性?难道传的不是基本数据类型,而是一个对象?于是看了下这个方法的源码:

    def set_network_connection(self, connectionType):        """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:         - connectionType - a member of the enum appium.webdriver.ConnectionType        """        data = {            'parameters': {                'type': connectionType.value            }        }        return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']

关键字:

 - connectionType - a member of the enum appium.webdriver.ConnectionType

翻译一下这个注释:

- connectionType - 枚举类型 appium.webdriver.ConnectionType 中的成员

然后看看 appium.webdriver.ConnectionType

...class ConnectionType(Enum):    NO_CONNECTION = 0    AIRPLANE_MODE = 1    WIFI_ONLY = 2    DATA_ONLY = 4    ALL_NETWORK_ON = 6

ok,明白了。就是 connectionType 必须是 appium.webdriver.ConnectionType 里的其中一个成员。接下来就简单了。

问题解决

正确的使用方法:

# python# set network  from appium.webdriver.connectiontype import ConnectionTypeself.driver.set_network_connection(ConnectionType.AIRPLANE_MODE)# get networkself.driver.network_connection # it would return int type, like 0, 1, 2, 4, 6ConnectionType(self.driver.network_connection).name # it would return mode name, like AIRPLANE_MODE, WIFI_ONLY

总结

这个其实本来没什么问题的,只是官方文档缺失导致用的时候要稍微探究一下才会用。同时也简单学习了一下怎么在 python 中使用枚举。


原创粉丝点击