示例代码和REST API的SDK封装
终端云测-兼容测试 menu

示例代码和REST API的SDK封装

更新于: 2022-11-16 11:36

WeTest云测提供了OpenAPI用于测试的提交,任务的查询,结果的拉取等,并且提供PHP/Python的SDK使用,如PythonSDK下载

安装依赖

python3 setup.py install

自动化兼容测试示例代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import time
import cloudtestapi as ct

# 配置
host = "https://env_host"
secret_id = "your_secret_id"
secret_key = "your_secret_ey"
PROJECT = 'your_project_id'

# 初始化客户端
client = ct.CloudTestAPI(host, secret_id, secret_key)
# 上传APP
app_id = client.upload_apk("demo.apk")["app"]["app_id"]
print("app id id is %d" %(app_id))
# 上传脚本
script_id = client.upload_script("demo.zip")["script"]["script_id"]
print("script id is %d" %(script_id))
# 创建测试
com_test = ct.CompatibilityTest()
com_test.set_project(PROJECT)
# 设置项目,设备云id、自动化框架、设备数等
com_test.set_app_id(app_id). \
    set_script_id(script_id). \
    set_cloud_ids([2]).\
    set_frame_type('appium').set_device_number(1,1)
# 提交测试
test = client.submit_compatibility_test(com_test)

# 等待测试结束
def wait_test_end(test, client):
    test_id = test["test_info"]["test_id"]
    start_time = time.time()
    while True:
        ret = client.get_test_status(test_id)
        if ret["ret"] != 0:
            print("get test status faild")
            break
        print(ret)
        if ret["test_status"]["finished"] == True:
            print("test end")
            break
        time.sleep(10)
    end_time = time.time()
    cost = end_time - start_time
    print("test cost ", cost)
print(test)
wait_test_end(test, client)

远程调试示例代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import time
import cloudtestapi as ct
import subprocess

# 配置
host = "https://env_host"
secret_id = "your_secret_id"
secret_key = "your_secret_ey"
PROJECT = 'your_project_id'


client = ct.CloudTestAPI(host, secret_id, secret_key)
def quit_if_error(resp):
    if resp["ret"] == 0:
        return
    print("error quit %s" %(resp))
    sys.exit(1)
def choose_free_device(devices):
    for d in devices:
        if d['device_state_code'] == 2:
            return d['device_id']
    return None

# 获取用户具有权限的云列表
print(client.get_user_clouds())
# 获取专有云设备列表
devices = client.get_cloud_devices(1)['devices']
# 开启远程调试,获取test_id
device_id = choose_free_device(devices)
if device_id == None:
    print("no free device")
    sys.exit(1)
resp = client.submit_debug_test(device_id, PROJECT)
test_id = resp["debug_test_info"]["test_id"]
print("device start test_id %d" %(test_id))
# 开启获取adb
resp = client.start_test_device_adb_debug(test_id, device_id)
quit_if_error(resp)
# 授权通过adb
resp = client.set_test_device_adb_auth(test_id, device_id)
quit_if_error(resp)
# 获取adb的地址,然后执行指令
resp = client.get_test_device_adb_status(test_id,device_id)
quit_if_error(resp)
adb_addr = resp["adb_status"]["addr"]
print("adb addr %s" %(adb_addr))
# 执行adb指令,和进行测试
print(subprocess.getoutput("adb connect %s" %(adb_addr)))
time.sleep(5)
print(subprocess.getoutput("adb -s %s ls /sdcard" %(adb_addr)))
time.sleep(5)
# 取消测试(取消占用设备)
print(client.cancel_test(test_id))
购买
客服
反馈