码迷,mamicode.com
首页 > 移动开发 > 详细

app专项测试之启动时间

时间:2018-02-16 18:10:36      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:event   当前时间   readline   post   pos   pre   循环   __name__   shell   

技术分享图片
# coding: utf-8
import os,csv,time
class App(object):
    def __init__(self):
        self.content=‘‘
        self.startTime=0

    #启动APP
    def launchApp(self):
        cmd=adb shell am start -W -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity
        self.content=os.popen(cmd)
        #停止APP
    def StopApp(self):
        cmd=adb shell am force-stop com.android.chrome
        # cmd=‘adb shell keyevent 3‘
        os.popen(cmd)
    def GetLaunchedTime(self):
        #循环带有ThisTime这一行
        for line in self.content.readlines():
            if ThisTimein line:
                self.startTime=line.split(:)[1]
                break
        return self.startTime

class Controller(object):
    def __init__(self,count):
        self.app=App()
        self.counter=count
        # timestamp:这次是从几分几秒开始的
        # elapsedtime耗时
        self.alldata = [("timestamp", "elapsedtime")]
        #单次测试过程
    def testprocess(self):
        self.app.launchApp()
        time.sleep(3)
        elpasedtime =self.app.GetLaunchedTime()
        self.app.StopApp()
        time.sleep(5)
        currenttime=self.getCurrentTime()
        self.alldata.append((currenttime,elpasedtime))
    def run(self):
        while self.counter>0:
            self.testprocess()
            self.counter=self.counter-1
    def getCurrentTime(self): #获取当前时间戳方法
        currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        #获取当前的时间戳
        return currentTime
    def SaveDataToCSV(self):
        csvfile=file(startTime.csv, wb)
        write=csv.writer(csvfile)
        write.writerows(self.alldata) #write.writerow是向一行写
        csvfile.close()

if __name__ == __main__:
    controller=Controller(5) #执行次数
    controller.run()
    controller.SaveDataToCSV()
启动时间
技术分享图片
第一步:创建两个类
      APP class: #干活的类
          LaunchApp
          StopApp
          GetlaunchTime

      Controller class:#调用的类
          run
          collectAllData
          SavaDataToCSV
-----------------------------------------
第二步:
  def launchApp(self): 启动APP方法
        cmd=adb shell am start -W -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity
        self.content=os.popen(cmd) 执行shell的模块命令,获取返回值content
   def StopApp(self): 停止APP方法
        cmd=adb shell am force-stop com.android.chrome
        os.popen(cmd)
第三步:因为 GetlaunchTime方法需要用到launchApp(self)的content所以需要初始化一个变量
class App():
    def __init__(self):
        self.content=‘‘

第四步:
 def GetLaunchedTime(self):
        for line in self.content.readlines(): content内容中循环
            if ThisTimein line: 如果这一行带有ThisTime
                self.startTime=line.split(:)[1]则把这一行以“:”切分,赋值给startTime
                break 跳出
        return self.startTime 返回ThisTime后的时间

第五步:初始化self.startTime=0以待其他方法使用。






第六步:Controller()类操作
    6.1class Controller():
        def __init__(self,count):
            self.app=App() #实例化后就可以在本类中使用其他类( app)的方法了。
            self.counter=count #设置运行的次数
    6.2:
    #单次测试过程
    def testprocess(self):
        self.app.launchApp()
        self.app.GetLaunchedTime()
        self.app.StopApp()

    6.3:多次执行的测试过程
        def run(self):
        while self.counter>0:
            self.testprocess()
            self.counter=self.counter-1
启动时间备注

 

app专项测试之启动时间

标签:event   当前时间   readline   post   pos   pre   循环   __name__   shell   

原文地址:https://www.cnblogs.com/wanghuaqiang/p/8450321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!