forked from openatx/atxserver2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
android_test.py
93 lines (75 loc) · 2.44 KB
/
android_test.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# coding: utf-8
#
import re
import subprocess
import time
from pprint import pprint
import requests
from logzero import logger
import uiautomator2 as u2
server_url = "http://localhost:4000"
token = "82d03bc6110145b2814e5e3c55bd8980" # change to your token
def make_url(path):
if re.match(r"^https?://", path):
return path
return server_url + path
def request_api(path, method="GET", **kwargs):
kwargs['headers'] = {"Authorization": "Bearer " + token}
r = requests.request(method, make_url(path), **kwargs)
try:
r.raise_for_status()
except requests.HTTPError:
pprint(r.text)
raise
return r.json()
def main():
# 获取用户信息
ret = request_api("/api/v1/user")
logger.info("User: %s", ret['username'])
# 获取可用设备列表
ret = request_api("/api/v1/devices", params={"usable": "true"})
if not ret['devices']:
raise EnvironmentError("No devices")
logger.info("Device count: %d", ret['count'])
# 占用设备
device = ret['devices'][0]
udid = device['udid']
logger.info("Choose device: \"%s\" udid=%s", device['properties']['name'],
udid)
ret = request_api("/api/v1/user/devices",
method="post",
json={"udid": udid})
print(ret)
try:
# 获取占用设备信息
ret = request_api("/api/v1/user/devices/" + udid)
source = ret['device']['source']
pprint(source)
# 安装应用
logger.info("install app")
provider_url = source['url']
ret = request_api(
provider_url + "/app/install",
method="post",
params={"udid": udid},
data={
"url":
"https://github.com/openatx/atxserver2/releases/download/v0.2.0/ApiDemos-debug.apk"
})
pprint(ret)
# 运行测试
adb_remote_addr = source['remoteConnectAddress']
subprocess.run(['adb', 'connect', adb_remote_addr])
time.sleep(1)
d = u2.connect_usb(adb_remote_addr)
print(d.info)
d.app_start("io.appium.android.apis")
d.xpath("App").click() # same as d(text="App").click()
logger.debug("Assert Alert button exists")
assert d(text="Alarm").wait()
finally:
# 释放设备
ret = request_api("/api/v1/user/devices/" + udid, method="delete")
print(ret)
if __name__ == "__main__":
main()