您的位置:首页 > 理论基础 > 计算机网络

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

2015-10-08 14:46 771 查看


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


前言

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


问题描述

appium 一直有一个设置和获取当前各项连接状态的 api ,官方文档中可用参数及使用方法如下:
Value (Alias)DataWifiAirplane Mode
0 (None)000
1 (Airplane Mode)001
2 (Wifi only)010
4 (Data only)100
6 (All network on)110
// javascript
// set airplane mode
driver.setNetworkConnection(1)

// set wifi only
driver.setNetworkConnection(2)

// set data only
driver.setNetworkConnection(4)

// set wifi and data
driver.setNetworkConnection(6)


但在 python client 中使用这样的代码会报错:
self.driver.set_network_connection(1)


报错信息:
Error
Traceback (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.value
AttributeError: '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 ConnectionType
self.driver.set_network_connection(ConnectionType.AIRPLANE_MODE)

# get network
self.driver.network_connection # it would return int type, like 0, 1, 2, 4, 6
ConnectionType(self.driver.network_connection).name # it would return mode name, like AIRPLANE_MODE, WIFI_ONLY


总结

这个其实本来没什么问题的,只是官方文档缺失导致用的时候要稍微探究一下才会用。同时也简单学习了一下怎么在 python 中使用枚举。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: