app自动化测试 启动时间 and cpu

本测试脚本是adb  +  python的基础上

app的启动分为冷启动和热启动两种 冷启动是app在退出的的情况下的启动  热启动是app在启动之后 退至后台运行的一种启动方式

首先 冷启动

在编写测试脚本之前 你需要了解的是获取电池电量的adb命令

启动app命令:adb shell am start -W -n package/activity

停止app命令:adb shell am force-stop package

获取package/activity的命令:adb logcat | grep START (点击app cmo=package/activity)

import os

import time

import csv

//app类

扫描二维码关注公众号,回复: 6094875 查看本文章

class APP():

        def __init__(self):

                self.cotent =  ""

               self.starttime = 0

        def LauchApp(self):

                cmd = "adb shell am  start   -W -n com.android.brower/.BrowerActivity"

                self.content = os.popen(cmd)

        def  StopApp(self):

               cmd = "adb shell am  force-stop com.android.brower/.BrowerActivity"

               os.popen(cmd)

       // 获取启动时间

         def GetLauchedTime(self):

                for line in self.content:

                     if "ThisTime"  in line:

                         self.starttime = line.split(":")[1]

                         break

                return self.starttime

//控制类

class Controller():

            def __init__(self,count):

                     self.alldata = [("timestamp","elapsedtime")]

                     self.count = count

                     self.app = APP()

             //单次测试

            def testprocess(self):

                     self.app.LauchApp()

                     time.sleep(5)

                     elapsedtime = self.app.GetLauchedTime()

                     time.sleep(3)

                     self.app.StopApp()

                     courrenttime = self.GetCurrentTime()

                     self.alldata.append((courrenttime,elapsedtime))

             def run(self):

                     while self.count >0 :

                                self.testprocess()

                                self.count = self.count - 1

             def GetCurrentTime(self):

                    currenttime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

                    return currenttime

             def SaveDataCsv(self):

                   csvwriter = file("start.csv","wb")

                   writer = csv.writer(csvwriter)

                   writer.writerows(self.alldata)

                   csvwriter.close()

cpu 自动化脚本

import os

import csv

import time

class controller():

         def __init__(self,counter):

                   self.counter = counter

                   self.alldata = [("timestamp","cpustatus")]

        // 单次测试

         def testprocess(self)

               result = os.popen(adb shell dumpsys cpuinfo | grep com.android.browser")

              for line in result.readlines():

                   cpuvalue = line.split("%")[0]

              currenttime = self.GetCurrentTime()

              self.alldata.append ((currenttime,cpuvalue))      

        def GetCurrentTime(self):

                    currenttime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

                    return currenttime

         def run(self):

               while self.counter > 0:

                       self.testprocess()

                       time.sleep(5)

                      self.counter = self.counter - 1

        

        def SaveDataCsv(self):

                   csvwriter = file("start.csv","wb")

                   writer = csv.writer(csvwriter)

                   writer.writerows(self.alldata)

                   csvwriter.close()

        

                   

猜你喜欢

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