adb测试-APP启动时间,写入csv

#! /usr/bin/env python
#! -*- cording:utf-8 -*-
import  os
import  time
import  csv

class App(object):
    def __init__(self):
        self.content=""
        self.starttime=""
    #启动APP
    def launchApp(self):
        cmd='adb shell am start -W -n com.sec.android.app.sbrowser/.SBrowserMainActivity'
        self.content=os.popen(cmd)
     #停止APP
    def StopApp(self):
        cmd ='adb shell am force-stop com.sec.android.app.sbrowser'
        os.popen(cmd)
    def GetLaunchedTime(self):
         for line in self.content.readlines():
             if "ThisTime" in line:
                 self.starttime=line.split(":")[1]
                 #拆分切片启动时间,取thistime后面的时间值
                 break
         return  self.starttime
#控制类
class Controller(object):
    def __init__(self,count):
        self.app=App()
        self.count=count
        self.alldata=[("timestamp","elapsedtime")]
#单次测试过程
    def testprocess(self):
        self.app.launchApp()
        elpasedtime=self.app.GetLaunchedTime()
        self.app.StopApp()
        currenttime=self.getCurrentTime()
        self.alldata.append((currenttime,elpasedtime))
#多次执行测试过程
    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
#根本原因是Python版本问题python2.x中要求用‘wb’,python3.x中要求用'w',如果写成wb则会报错,要写成w
    #2.75可以写wb
    def SaveDataToCSV(self):
        with open("startTime.csv","w") as f:
            writer = csv.writer(f)
            writer.writerows(self.alldata)
#写入



if __name__=='__main__':
    controller=Controller(3)
    controller.run()
    controller.SaveDataToCSV()

 

posted on 2017-08-27 17:38  aqiu  阅读(191)  评论(0)    收藏  举报

导航