Appium系列分享
Appium自动化(七)通过脚本自动化获取apk的包名和对应启动activity
Appium自动化(八)通过脚本自动化获取设备deviceName和platformVersion
Appium自动化(十二)Appium+unittest组织测试用例
Appium系列(十七)将Appium服务端口号通过参数传递给测试用例
Appium系列(二十一)优化待测apk的传入和desired_caps内容优化
Appium系列(二十一)操作IOS设备利器——tidevice
Appium系列(二十二)基于tidevice封装IOS的adb命令
前言
前一篇文章,我们介绍了搭建stf设备管理平台的技能。那么我们的测试框架怎么来用呢,其实也是很简单的。我们需要远程去操作链接设备平台。那么我们应该怎么去操作stf呢。本文带你揭秘。
正文
我们现在的平台和框架要打通,stf提供了开放的api接口。我们可以直接利用这些接口。在远程调用的时候呢,我们需要一个token,我们需要在stf配置一个通用的token。
点击Settings,选择keys。
然后点击+
然后点击Generats,创建。
然后复制出来给到key,注意key只会展示一次,我们需要记录下来。
我们可以使用crul去测试
curl -H "Authorization: Bearer a2fa2821826a417cb65b7bf3308b61c54b0e6c051d1848f9a1559593a4a2ced4" http://localhost:7100/api/v1/devices
返回的结果:我本地没有设备,所以返回空。
那么接下来我们就是去封装一些我们操作stf的一些方法了。
我们需要在配置中配置token和stf服务地址
Test_stf_plan="http://localhost:7100"
Test_stf_token='a2fa2821826a417cb65b7bf3308b61c54b0e6c051d1848f9a1559593a4a2ced4'
那么我们接下来去封装一些方法,方便我们的操作,关于接口的,我在之前的文章中有分享。mac搭建stf设备集群管理平台。封装如下。
from config import Test_stf_plan, Test_stf_token
import requests, json
class StfPhoneOpear(object):
def __init__(self):
self.test_stf_plan = Test_stf_plan
self.test_stf_token = Test_stf_token
self.headers = {"Authorization": "Bearer " + Test_stf_token}
def getstflist(self) -> list:
'''
获取所有的设备
:return:
'''
requests_list = requests.get(Test_stf_plan + "/api/v1/devices", headers=self.headers).text
allreslut = json.loads(requests_list)
list_phone = allreslut['devices']
all_list_dict = []
for item in list_phone:
iphone = {}
iphone['url'] = item['remoteConnectUrl']
iphone["platform"] = item['platform']
iphone['version'] = item['version']
iphone['serial'] = item['serial']
iphone['use'] = item['using']
all_list_dict.append(iphone)
return all_list_dict
def getonedetail(self, name) -> dict:
'''
获取设备详情
:param name:
:return:
'''
requests_list = requests.get(Test_stf_plan + "/api/v1/devices/" + name, headers=self.headers).text
allreslut = json.loads(requests_list)
item = allreslut['devices']
iphone = {}
iphone['url'] = item['remoteConnectUrl']
iphone["platform"] = item['platform']
iphone['version'] = item['version']
iphone['serial'] = item['serial']
return iphone
def getoneconnecturl(self, name):
'''
获取设备远程操作地址
:param name:
:return:
'''
url = Test_stf_plan + "/api/v1/user/devices/" + name + '/remoteConnect'
requests_list = requests.post(url,
headers=self.headers).text
allreslut = json.loads(requests_list)
item = allreslut['remoteConnectUrl']
return item
def oparyonephone(self, name) -> bool:
'''
申请设备
:param name:
:return:
'''
data = {"serial": name}
self.headers['Content-Type'] = 'application/json'
requests_list = requests.post(url=Test_stf_plan + "/api/v1/user/devices",
headers=self.headers, data=json.dumps(data)).text
allreslut = json.loads(requests_list)
if allreslut['success'] is True:
return True
return False
def removeroneophen(self, name) -> bool:
'''
归还设备
:param name:
:return:
'''
requests_list = requests.delete(Test_stf_plan + "/api/v1/user/devices/" + name,
headers=self.headers).text
allreslut = json.loads(requests_list)
if allreslut['success'] is True:
return True
return False