Monkeyrunner 编写自动化脚本

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
import os
import datetime
import time
import shutil
import subprocess


class Collect_Infromation():
    def __init__(self,host):
        self.host = host
        self.collect_information = "collect_information"
        self.packagename = None
        self.host = None
        self.pid = None
        self.packagename_activity = None

    def device_power_information(self,host):
        cmds = []
        if host == None:
            content_information = os.popen('adb shell dumpsys batterystats | more').readlines()
        else:
            cmds.append('adb -s %s shell dumpsys batterystats | more' %host)
            print(cmds)
            for adb_path in cmds:
                content_information = os.popen(adb_path).readlines()
        return content_information

    def runing_pacename_and_activity_name(self,host):
        cmds = []
        if host == None:
            content_information = os.popen("adb shell 'logcat | grep START'").readlines()
        else:
            cmds.append('adb -s %s shell logcat | grep START' %host)
            for adb_path in cmds:
                content_information = os.popen(adb_path).readlines()
        return content_information

    def start_app(self,packagename_activity,host):
        cmds = []
        if host == None:
            content_information = os.popen("adb shell am start -W -n " + packagename_activity)
        else:
            cmds.append('adb -s %s shell am start -W -n %s'%host%packagename_activity)
            for adb_path in cmds:
                content_information = os.popen(adb_path).readlines()
        return content_information

    def kill_app (self,packagename,host):
        cmds = []
        if host == None:
            content_information = os.popen("adb shell am force-stop "+packagename)
        else:
            cmds.append('adb -s  %s shell am force-stop  %s' %packagename%host)
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def backgrounder_run(self,host,packagename):
        self.packagename = packagename
        cmds = []
        if host == None:
            content_information = os.popen("adb shell am force-stop "+self.packagename)
        else:
            cmds.append("adb -s %s shell am force-stop %s" %host%self.packagename)
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def cpu_information(self,host,packagename):
        self.packagename = packagename
        cmds = []
        if host == None:
            content_information = os.popen("adb shell dumpsys cupinfo | grep" + self.packagename).readlines()
        else:
            cmds.append("adb -s %s shell dumpsys cupinfo | grep %s" %(host,self.packagename))
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def pid_information(self,packagename,host):
        self.packagename = packagename
        cmds = []
        if host == None:
            content_information = os.popen("adb shell ps | grep " + packagename).readlines()
        else:
            cmds.append("adb -s %s shell ps | grep  %s" %host%self.packagename)
            for adb_path in cmds:
                content_information = os.popen(adb_path).readlines()
        return content_information

    def flow_information(self,pid,host):
        self.pid = pid
        cmds = []
        if host == None :
            adb_path = 'adb shell cat /proc/'+str(self.pid) +'/status'
            content_information = os.popen(adb_path).readlines()
        else:
            cmds.append("adb -s  %s shell cat /proc/%s/status" %host%self.pid)
            for adb_path in cmds:
                content_information = os.popen(adb_path).readlines()
        return content_information

    def off_power(self,host):
        self.host = host
        cmds = []
        if self.host == None:
            content_information = os.popen('adb shell dumpsys battery set status 1')
        else:
            cmds.append('adb -s %s shell dumpsys battery set status 1' %host)
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def on_power(self,host):
        self.host = host
        cmds = []
        if self.host == None:
            content_information = os.popen('adb shell dumpsys battery set status 2')
        else:
            cmds.append("adb -s  %s shell dumpsys battery set status 2")
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def battery_information(self,host):
        cmds = []
        if host == None:
            content_information = os.popen('adb shell dumpsys battery').readlines()
        else:
            cmds.append("adb -s %s shell dumpsys battery")
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def app_power_information(self,packagename,host):
        self.packagename = packagename
        cmds = []
        if host == None:
            content_information = os.popen('adb shell dumpsys batterystats'+self.packagename + '|' + 'more').readlines()
        else:
            cmds.append("adb -s  %s shell dumpsys batterystats  %s | more" %host%packagename)
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def set_size(self,host):
        cmds = []
        if host == None:
            content_information = os.popen('adb shell top')
        else:
            cmds.append("adb -s %s shell top")
            for adb_path in cmds:
                content_information = os.popen(adb_path)
        return content_information

    def pull_information (self,host):
        if host == None:
            os.popen("adb pull xxx   xxx")
        else:
            adb_path = 'adb -s ' + host + "pull xxx xxx "
            os.popen(adb_path)

    def make_dir(self,host):

        if os.path.exists("D:\\workspacetest\\" + host):
            shutil.rmtree("D:\\workspacetest\\" + host)
            os.mkdir("D:\\workspacetest\\" + host)
            result = "D:\\workspacetest\\" + host + "\\"

        else:
            os.mkdir("D:\\workspacetest\\" + host)
            result = "D:\\workspacetest\\" + host + "\\"
        return result

    def screen_shot(self,host):
        cmds = []
        if host == None:
            os.popen('adb shell screencap -p /sdcard/%s.png' %host)
            os.popen('adb pull /sdcard/1.png  D:\\workspacetest\\%s\\' %(host,host))
        else:
            cmds.append('adb -s %s pull /sdcard/1.png  D:\\workspacetest\\%s\\' %(host,host))
            for adb_path in cmds:
                os.popen('adb -s %s shell screencap -p /sdcard/1.png' %host)
                os.popen(adb_path)

    def screen_record(self,host):
        cmds = []
        if host == None:
            os.popen('adb shell screenrecord --time-limit 10 --verbose /sdcard/demo.mp4')
            os.popen('adb pull /sdcard/demo.mp4 D:\\workspacetest\\%s\\' %(host,host))
        else:
            cmds.append('adb -s %s pull /sdcard/demo.mp4  D:\\workspacetest\\%s\\' %(host,host))
            for adb_path in cmds:
                os.popen('adb -s %s shell screenrecord --time-limit 3 --verbose /sdcard/demo.mp4' %host)
                os.popen(adb_path)


host_list = ["cfc807b0","cfcd0678"]
name_list = "150026021"
name_password = "12345678"

for host in host_list:
    device2 = MonkeyRunner.waitForConnection(5,host)
    result2_before = device2.takeSnapshot()
    collect = Collect_Infromation(host)
    collect.screen_record(host)
    result = collect.make_dir(host)
    information = collect.device_power_information(host)
    print(information)
    collect.screen_shot(host)
    MonkeyRunner.sleep(1.0)
    result2_before.writeToFile(result + 'before' + '.png', 'png')
    device2.touch(1232,33,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.touch(1200,700,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.touch(530,153,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.type(name_list)
    device2.touch(568,230,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.type(name_password)
    result2_during = device2.takeSnapshot()
    result2_during.writeToFile(result + 'during' + '.png', 'png')
    device2.touch(700,330,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.touch(600,300,"DOWN_AND_UP")
    MonkeyRunner.sleep(3.0)
    device2.touch(28,33,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.touch(1236,28,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.touch(1164,688,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    device2.touch(744,426,"DOWN_AND_UP")
    MonkeyRunner.sleep(1.0)
    result2_after = device2.takeSnapshot()
    result2_after.writeToFile(result + 'after' + '.png', 'png')
    print("*"*100)

猜你喜欢

转载自blog.csdn.net/weixin_41918727/article/details/86477653