每次使用设备前重置云手机

This commit is contained in:
weitonhuang 2021-09-30 10:34:11 +08:00
parent 29fa3e017a
commit 516ee7e8da
3 changed files with 128 additions and 0 deletions

95
scripts/huawei_util.py Normal file
View File

@ -0,0 +1,95 @@
import json
import requests
def get_phone_name(index):
if index < 10:
return "cph-v9xc-1-0000" + str(index)
return "cph-v9xc-1-000" + str(index)
def get_phones():
url = "https://cph.cn-south-1.myhuaweicloud.com/v1/0a82da3dec00f43a2f45c004d252ae44/cloud-phone/phones"
params = {
"server_id": "1106f4edf47c4e8f9130a021f67dc447",
}
headers = {
"X-Auth-Token": get_token()
}
phone_list_res = requests.get(url=url, params=params, headers=headers)
phone_list = json.loads(phone_list_res.text)["phones"]
phone_map = {}
for phone in phone_list:
phone_map[phone["phone_name"]] = phone["phone_id"]
return phone_map
def reset_phone(phone_id):
url = "https://cph.cn-south-1.myhuaweicloud.com/v1/0a82da3dec00f43a2f45c004d252ae44/cloud-phone/phones/batch-reset"
params = {
"phones": [
{
"phone_id": phone_id
}
]
}
headers = {
"X-Auth-Token": get_token()
}
r = requests.post(url=url, data=json.dumps(params), headers=headers)
print(r.text)
print("=============================================")
def phone_reset_finish(phone_id):
url = "https://cph.cn-south-1.myhuaweicloud.com/v1/0a82da3dec00f43a2f45c004d252ae44/cloud-phone/phones/" + phone_id
headers = {
"X-Auth-Token": get_token()
}
phone_state_res = requests.get(url=url, headers=headers)
phone_state = json.loads(phone_state_res.text)
return phone_state["status"] == 2
def get_token():
url = "https://iam.cn-south-1.myhuaweicloud.com/v3/auth/tokens"
data = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "hw_008613632826027_01",
"password": "Zz102938",
"domain": {
"name": "hw_008613632826027_01"
}
}
}
},
"scope": {
"project": {
"name": "cn-south-1"
}
}
}
}
headers = {
"Content-Type": "application/json",
}
r = requests.post(url=url, data=json.dumps(data), headers=headers)
return r.headers.get("X-Subject-Token")
if __name__ == '__main__':
print(get_token())
# print("================================================")
# print(get_phones())
# print("================================================")
# # reset_phone("300295ccf4c94c67879069eb35527756")
# print(phone_reset_finish("300295ccf4c94c67879069eb35527756"))

View File

@ -93,6 +93,24 @@ sleep 20
# install New Monkey
adb -s $AVD_SERIAL install -g $NEWMONKEY_TOOL/NewMonkey-3.4.apk
echo "** INSTALL NewMonkey (${AVD_SERIAL})"
RETRY_TIMES=10
for i in $(seq 1 $RETRY_TIMES);
do
echo "wait when new monkey installing on (${AVD_SERIAL})..."
sleep 5
install_success=`adb -s $AVD_SERIAL shell pm list package | grep newmonkey`
echo "install_success:${install_success}"
# validate whether the target app is running
if [[ install_success != "" ]]
then
break
else
echo "*****reinstall new monkey apk ...******"
adb -s $AVD_SERIAL install -g $NEWMONKEY_TOOL/NewMonkey-3.4.apk
fi
done
# get app package
app_package_name=`aapt dump badging $APK_FILE | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
echo "** PROCESSING APP (${AVD_SERIAL}): " $app_package_name

View File

@ -3,6 +3,8 @@ import time
from argparse import ArgumentParser, Namespace
from multiprocessing.pool import ThreadPool
import huawei_util
def get_time_in_seconds(testing_time):
if 'h' in testing_time:
@ -194,11 +196,14 @@ def main(args: Namespace):
# allocate emulators for an apk
start_avd_serial = args.offset if args.offset > 0 else 1
avd_serial_list = []
phone_index_name_map = {}
for apk_index in range(args.number_of_devices):
avd_serial = connect_ssl(start_avd_serial + apk_index)
connect_adb(avd_serial)
# avd_serial = 'emulator-' + str(start_avd_serial + apk_index * 2)
avd_serial_list.append(avd_serial)
phone_index_name_map[avd_serial] = huawei_util.get_phone_name(start_avd_serial + apk_index)
print(phone_index_name_map[avd_serial])
print('allocate emulators: %s' % avd_serial)
if args.no_headless:
@ -244,6 +249,16 @@ def main(args: Namespace):
login_script = all_apks_login_scripts[apk_index]
print("its login script: %s" % login_script)
# 重置云手机
phone_id = huawei_util.get_phones()[phone_index_name_map[avd_serial]]
print("reset phone " + phone_id)
huawei_util.reset_phone(phone_id)
# 请求发送后, 设备状态不会马上改变, 需要等一会
time.sleep(10)
while not huawei_util.phone_reset_finish(phone_id):
print("wait phone reset finish")
time.sleep(5)
if args.monkey:
p.apply_async(run_monkey, args=(current_apk, avd_serial, args.avd_name,
args.o, args.time, screen_option,