APP自动化-ATX集成到代码中

把atx的框架集成到代码中

把ATX-API的脚本复制到已有项目中

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # @Time    : 2023/6/13 10:29
  4 # @Author  : gezirui
  5 # @File    : package_atx_api.py
  6 # @Software: PyCharm
  7 from requests import get, post, delete
  8 import random
  9 import time
 10 
 11 
 12 class AtxServer:
 13     def __init__(self, host, token):
 14         self.host = host
 15         self.token = token
 16         self.headers = {"Authorization": f'Bearer {self.token}'}
 17         self.uuid = ""
 18         self.using_device_info = {}
 19 
 20     def get_user(self):
 21         user_data = get(self.host + "/api/v1/user", headers=self.headers).json()
 22 
 23         return user_data
 24 
 25     def get_all_devices(self):
 26         all_devices = get(self.host + "/api/v1/devices", headers=self.headers).json()
 27 
 28         return all_devices
 29 
 30     def get_no_using_devices(self, all_devices):
 31         no_using_devices = []
 32         for i in all_devices["devices"]:
 33             present = i.get("present")
 34             using = i.get("using")
 35             colding = i.get("colding")
 36             if present == True and using == 0 and not colding:
 37                 # print("设备正在空闲状态中")
 38                 udid = i["udid"]
 39                 no_using_devices.append(udid)
 40 
 41         return no_using_devices
 42 
 43     def random_one_device(self, no_using_devices):
 44         self.udid = random.choice(no_using_devices)
 45 
 46         return random.choice(no_using_devices)
 47 
 48     def using_device(self, udid=None):
 49 
 50         if udid:
 51             self.udid = udid
 52 
 53         result = post(self.host + "/api/v1/user/devices",
 54                       headers=self.headers,
 55                       json={"udid": self.udid}).json()
 56 
 57         return result
 58 
 59     def get_using_device_info(self, udid=None):
 60         if udid:
 61             self.udid = udid
 62 
 63         result = get(self.host + "/api/v1/user/devices/" + self.udid,
 64                      headers=self.headers).json()
 65         self.using_device_info = result
 66         return result
 67 
 68     def get_remote_connect_address(self):
 69         sources = self.using_device_info["device"]["sources"]
 70         # print("1:", sources.values)
 71         # print("2:", list(sources.values())[0])
 72         self.remoteConnectAddress = list(sources.values())[0]["remoteConnectAddress"]
 73 
 74     def release_device(self, udid=None):
 75         if udid:
 76             self.udid = udid
 77 
 78         result = delete(self.host + "/api/v1/user/devices/" + self.udid,
 79                         headers=self.headers).json()
 80         return result
 81 
 82 
 83 if __name__ == '__main__':
 84     host_ = "http://localhost:4000"
 85     token_ = "dd7fe77f7b9c400cabd29514a8341a6b"
 86     atx = AtxServer(host=host_, token=token_)
 87     all_devices = atx.get_all_devices()
 88     print("所有设备:", all_devices)
 89     no_using_devices = atx.get_no_using_devices(all_devices)
 90     print("udid列表:",no_using_devices)
 91     atx.random_one_device(no_using_devices)
 92     print("随机设备是:" ,atx.random_one_device(no_using_devices))
 93     atx.using_device()
 94     print("使用设备:",atx.using_device())
 95     atx.get_using_device_info()
 96     print("使用设备详情:",atx.get_using_device_info())
 97     atx.get_remote_connect_address()
 98     print("远程连接地址:", atx.remoteConnectAddress)
 99     time.sleep(5)
100     atx.release_device()
101     print("释放情况:",atx.release_device())
View Code

 运行下ATX-API的脚本确定正常,可以正常的查找设备列表,连接设备,释放设备等操作

然后复制要封装的关键字部分到keyword文件中,如def content_atx_devic

host_ = "http://localhost:4000"
    token_ = "dd7fe77f7b9c400cabd29514a8341a6b"
    atx = AtxServer(host=host_, token=token_)
    all_devices = atx.get_all_devices()
    print("所有设备:", all_devices)
    no_using_devices = atx.get_no_using_devices(all_devices)
    print("udid列表:",no_using_devices)
    atx.random_one_device(no_using_devices)
    print("随机设备是:" ,atx.random_one_device(no_using_devices))
    atx.using_device()
    print("使用设备:",atx.using_device())
    atx.get_using_device_info()
    print("使用设备详情:",atx.get_using_device_info())
    atx.get_remote_connect_address()
    print("远程连接地址:", atx.remoteConnectAddress)
    time.sleep(5)
    atx.release_device()
    print("释放情况:",atx.release_device())

为了方便关联excel表格中已有的CAPS信息的值获取,不同设备之间也就更改平台、平台版本、平台名字,

所以在ATX-API文件中AtxServer类下新建一个获取CAPS信息的函数,替代使用设备详情和远程连接地址信息的函数

    def get_device_caps(self):
        sources = self.using_device_info["device"]["sources"]
        self.remote_connect_address = list(sources.values())[0]["remoteConnectAddress"]
        self.platform_name = self.using_device_info["device"]["platform"]
        self.platform_version = self.using_device_info["device"]["properties"]["version"]

分别获取sources、remote_connect_address、platform_name 、platform_version信息。

在关键字函数文件Keywords中编辑函数def content_atx_device,设置远程连接信息,(释放设备函数需要单独拿出来)

def content_atx_device(url, caps, atx_url, atx_token):   # 连接ATX设备(用这个函数替代excel表格中的session)
    caps = json.loads(caps) #用loads将json编码成python
    host_ = atx_url
    # host_ = "http://localhost:4000"
    # token_ = "dd7fe77f7b9c400cabd29514a8341a6b"
    token_ = atx_token
    atx = AtxServer(host=host_, token=token_)   # 用集群设备函数关联excel表格中的值
    setattr(Context, "atx", atx)    # 设定Contex类中的“atx”对象的值是atx对象,用于传给释放设备函数使用
    all_devices = atx.get_all_devices()
    print("所有设备:", all_devices)
    no_using_devices = atx.get_no_using_devices(all_devices)
    print("udid列表:", no_using_devices)
    atx.random_one_device(no_using_devices)
    print("随机设备是:", atx.random_one_device(no_using_devices))
    atx.using_device()
    print("使用设备:", atx.using_device())
    atx.get_using_device_info()
    print("使用设备详情:", atx.get_using_device_info())
    atx.get_device_caps()
    print("远程连接地址:", atx.remote_connect_address)
    # time.sleep(5)
    # atx.release_device()
    # print("释放情况:", atx.release_device())  # 释放设备单独作为关键字运行,这里注释

    if caps.get("appium.platformVersion", None):    # 如果中的CAPS里的"appium.platformVersion"为为空,取atx远程连接信息的值
        caps.update({"appium.platformVersion": atx.platform_version})
    else:                                           # 如果中的CAPS里的"appium.platformVersion"有值,就用表格中的
        caps.update({"platformVersion": atx.platform_version})
    if caps.get("platformName", None):
        caps.update({"platformName": atx.platform_name})
    if caps.get("appium:deviceName",None):
        caps.update({"appium:deviceName": atx.remote_connect_address})
    else:
        caps.update({"deviceName": atx.remote_connect_address})
    session(url=url, caps=json.dumps(caps)) #用dumps将python编码成json字符串

创建新的释放设备函数

def atx_release_device():
    atx = getattr(Context, "atx")
    atx.release_device()
    time.sleep(10)  # 释放时间过长,这里10S差不多

 

修改excel表格信息中的值,保持关键字函数和表格一致。content_atx_device替代原来的session关键字,最后设置释放设备关键字。

 运行后成功。

 

posted @ 2023-06-13 19:09  琉璃星眸  阅读(34)  评论(0编辑  收藏  举报