开源云真机平台-Sonic平台-python自定义脚本(持续更新中)

开源云真机平台-Sonic平台-python自定义脚本(持续更新中)

1、获取设备序列号

udId = sys.argv[1:][1]

如:

# -*- coding: utf-8 -*- 
  
import os,sys,json
udId = sys.argv[1:][1]

2、获取全局参数

text1 = sys.argv[1:][2]

如:

# -*- coding: utf-8 -*- 
  
import os,sys,json
text1 = sys.argv[1:][2]

 3、获取全局参数后,将其转换为JSON格式

备注:获取全局参数后,得到的是text类型的字符串,使用切割的方式将其转化为JSON格式;

# -*- coding: utf-8 -*- 
  
import os,sys,json
udId = sys.argv[1:][1]
text1 = sys.argv[1:][2]

print(f"udId={udId},text={text1},type={type(text1)}")
print("="*30)
data = text1.strip('{} ')
pairs = data.split(',')
result = {}
for pair in pairs:
    key, value = pair.split(':')
    result[key] = value
print(f"result={result}")
print("="*30)

4、自动开启WIFI

# -*- coding: utf-8 -*- 
  
import os,sys,json
udId = sys.argv[1:][1]
text1 = sys.argv[1:][2]

def run_command(command):
    # command = f"""adb -s {udId} shell settings put system screen_brightness 0"""
    print(f"command={command}")
    os.system(command)
  
def run_test(udId):
    run_command(f"""adb -s {udId} shell svc wifi enable""")
          
print(f"udId={udId},text={text1},type={type(text1)}")
print("="*30)

run_test(udId)

5、adb模拟滑动

import os,sys,json,time
udId = sys.argv[1:][1]
text1 = sys.argv[1:][2]

command=f"""adb -s {udId} shell input swipe 165 700 165 100"""
print(f"command={command}")
os.system(command)
time.sleep(1)

6、执行monkey稳定性测试并将日志写在安卓设备内部

备注:

1)执行前,将屏幕亮度调到最低,将WIFI开启,将系统音量调到最低,

2)清除之前的旧日志,防止数据混淆;

3)将顶部状态栏禁用掉,可以一定程度上防止WIFI被关闭导致的测试问题(手动可以下拉状态栏);

4)清除logcat日志,便于记录新的日志;

# -*- coding: utf-8 -*- 
  
import os,sys,json,time
udId = sys.argv[1:][1]
text1 = sys.argv[1:][2]

def run_command(command):
    # command = f"""adb -s {udId} shell settings put system screen_brightness 0"""
    print(f"command={command}")
    os.system(command)
    time.sleep(1)
  
def run_monkey_test(udId):
    run_command(f"""adb -s {udId} shell pm setsystemuimode 0""")
    run_command(f"""adb -s {udId} shell svc wifi enable""")
    run_command(f"""adb -s {udId} shell settings put system screen_brightness 0""")
    run_command(f"""adb -s {udId} shell settings put system volume_music_min 0""")
    run_command(f"""adb -s {udId} shell settings put system volume_alarm_min 0""")
    run_command(f"""adb -s {udId} shell settings put global policy_control immersive.status=*""")
    run_command(f"""adb -s {udId} logcat -c""")
    run_command(f"""adb -s {udId} shell rm /sdcard/MonkeyLog.text""")
    run_command(f"""adb -s {udId} shell rm /sdcard/MonkeyError.text""")
    
    # run_command(f"""adb -s {udId} shell settings put global policy_control null""")
    run_command(f"""adb -s {udId} shell "monkey -p com.android.settings --pct-syskeys 0 --pct-touch 16 --pct-motion 16 --pct-trackball 16 --pct-majornav 0 --pct-appswitch 16 --pct-nav 16 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --kill-process-after-error -v -v -v --throttle 1000 9999999 -s 123456 1> /sdcard/MonkeyLog.text 2> /sdcard/MonkeyError.text &""")

    print(f"monkey testing...")
          
print(f"udId={udId},text={text1},type={type(text1)}")
print("="*30)

run_monkey_test(udId)

7、停止正在运行的monkey稳定性测试

备注:

1)判断是否存在monkey进程,若存在,则将其停止;

2)执行完毕后,恢复底部状态栏可见;

# -*- coding: utf-8 -*- 
  
import os,sys,json
import subprocess

udId = sys.argv[1:][1]

def run_command(cmd):
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    output, error = process.communicate()
    return output.decode().strip()


def check_and_kill_pid(udId):
    #read
    run_command(f'adb -s {udId} shell ps > shellps.log')

    #check
    with open('shellps.log', 'r') as file:
        for line in file:
            if 'com.android.commands.monkey' in line:
                apk_pin = line.split()[1]
                break
        else:
            apk_pin = None

    #kill
    if apk_pin:
        print(f"Monkey process PID : {apk_pin}")
        run_command(f'adb -s {udId} shell kill {apk_pin}')
        print("Monkey test end.")
    else:
        print("android device not run monkey test.")


check_and_kill_pid(udId)

command = f"""adb -s {udId} shell settings put global policy_control null"""
print(f"command={command}")
os.system(command)

运行效果:

8、[批量远程安装APK]批量安装局域网内共享目录下的APK文件

用途:局域网共享APK文件,将其批量安装到所有的安卓设备上;

备注:使用前需要保证局域网文件能够访问,需要设置带密码保护的共享;

# -*- coding: utf-8 -*-
import os
import subprocess
import os, sys, json, time, random

udId = sys.argv[1:][1]
text1 = sys.argv[1:][2]

ftp_path = r"\\10.1.100.1\share\APK"
temp_path = "/data/local/tmp/"
apk_name = "com.baidu.apk"
local_pc_path = r"D:\temp"

pc_path = os.path.join(ftp_path, apk_name)
android_path = os.path.join(temp_path, apk_name)

print("ftp_path=" + ftp_path)
print("pc_path=" + pc_path)
print("android_path=" + android_path)


def run_command(command):
    print(f"command={command}")
    os.system(command)
    time.sleep(1)


def run_test(udId):
    run_command(f"""adb -s {udId} shell rm {android_path}""")
    run_command(f"""adb -s {udId} shell ls -ll {android_path}""")
    if not os.path.exists(f"{local_pc_path}"):
        os.mkdir(local_pc_path)
    if not os.path.exists(fr"{local_pc_path}\{apk_name}"):
        run_command(f"""copy {pc_path} {local_pc_path}\.""")
        delay_time = random.randint(1, 10)
        time.sleep(delay_time)
        run_command(f"""dir {local_pc_path}""")
    else:
      time.sleep(15)

    if not os.path.exists(fr"{local_pc_path}\{apk_name}"):
        run_command(f"""copy {pc_path} {local_pc_path}\.""")
        delay_time = random.randint(1, 10)
        time.sleep(delay_time)
        run_command(f"""dir {local_pc_path}""")
    else:
      time.sleep(5)

    run_command(f"""adb -s {udId} push "{local_pc_path}\{apk_name}" {temp_path}.""")
    run_command(f"""adb -s {udId} shell sleep 3""")

    run_command(f"""adb -s {udId} shell ls -ll {android_path}""")
    run_command(f"""adb -s {udId} shell pm install -r {android_path}""")
    print(f"apk install successfully...")
    time.sleep(3)


if __name__ == '__main__':
    run_test(udId)







  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

《代码爱好者》

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值