导语

今天继续完成appium功能测试脚本向wda迁移过程中,遇到一个问题,无法获取switch的属性值,因为在此client端当前未有,所以今天的任务是写一个类似appium的方法————get_attribute()。


一、查看源码

FBElementCommands.m内有一个获取控件元素属性值的请求,

1
2
[[FBRoute GET:@"/element/:uuid/attribute/:name"] respondWithTarget:self
action:@selector(handleGetAttribute:)],

查看方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
+ (id<FBResponsePayload>)handleGetAttribute:(FBRouteRequest *)request
{
FBElementCache *elementCache = request.session.elementCache;
XCUIElement *element = [elementCache
elementForUUID:request.parameters[@"uuid"]];
id attributeValue = [element
fb_valueForWDAttributeName:request.parameters[@"name"]];
attributeValue = attributeValue ?: [NSNull null];
return FBResponseWithStatus(FBCommandStatusNoError, attributeValue);
}
+ (id<FBResponsePayload>)handleGetText:(FBRouteRequest *)request
{
FBElementCache *elementCache = request.session.elementCache;
XCUIElement *element = [elementCache
elementForUUID:request.parameters[@"uuid"]];
id text;
if ([element elementType] == XCUIElementTypeStaticText || [element
elementType] == XCUIElementTypeButton) {
text = [element wdLabel];
} else {
text = [element wdValue];
}
text = text ?: [NSNull null];
return FBResponseWithStatus(FBCommandStatusNoError, text);
}

看的不是很明白,但是确认的是GET请求,uuidelementid,这里的:name是什么呢?

二、借鉴appium

appium也是通过请求进行通信,同样有获取get_attribute()的方法,写个方法,打印出请求地址:
/element/elementid/attribute/value,此时,已经很简单了,直接命令行发起请求:

1
2
3
curl -X GET -H "Content-Type: application/json" http://192.168.1.101:8100/
session/C8C847CF-AD3D-4F5F-B51B-EA67788875DE/element/
A8C847CF-AD3D-4F5F-B51B-EA677DD885DE/attribute/value

有正确的返回值:

1
2
3
4
5
{
value:True,
sessionid:C8C847CF-AD3D-4F5F-B51B-EA67788875DE,
status:0
}

三、构造方法实现

现在实现已经很简单了:

1
2
3
4
5
6
def _property(self, name, data='', method='GET', timeout=None, eid=None):
eid = eid or self.wait(timeout)['ELEMENT']
return self._request(data, suburl='element/%s/%s' % (eid, name),
method=method)['value']
def attribute(self, name):
return self._property('attribute/%s' % name)

目前为止,appium功能测试脚本向wda迁移还是比较顺利的。。。。。。