python-appium监测APP启动用时

需求:模拟用户打开APP的操作,记录启动用时

# -*- coding: utf-8 -*-
# @Author  : sunyue

'''
本脚本测试app的单个冷启动时间
整体思路:启动前记录当前时间,启动后识别到目标控件记录当前时间,两次时间相减即为启动时间
'''

from appium import webdriver
from datetime import datetime
import time
from selenium.webdriver.support.ui import WebDriverWait

class bv_start:
    def platform(self, Version, Package, Activity):
        desired_caps={}
        desired_caps['platformName'] = 'Android'      #平台名称
        desired_caps['deviceName'] = 'aaa'         #设备名称
        desired_caps['platformVersion'] = Version     #平台版本
        desired_caps['appPackage'] = Package              #包名
        desired_caps['appActivity'] = Activity             #包入口
        desired_caps['noReset'] = True                 #不重置状态
        driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)  #启动服务/通过4723端口来建立一个会话
        self.driver = driver
        time.sleep(3)       #等待3s

    def execute(self, t, name, check):
        '''
        :param t: 表示需要测试的次数,
        :param name:测试应用安装在手机上后在手机上显示的应用名称
        :param check:识别的控件,设置需要识别的控件,识别到就记录当前时间
        '''
        self.driver.keyevent(3)
        Total_time = []
        for i in range(t):
            WebDriverWait(self.driver, 60).until(lambda x: x.find_element_by_android_uiautomator('new UiSelector().text("%s")'%(name)))
            self.driver.find_element_by_android_uiautomator('new UiSelector().text("%s")'%(name)).click()      #点击启动APP
            start_time = datetime.now()                #获取启动前用时
            while True:                           #循环的查找界面上的控件直到找到需要的停止并记录时间
                Str = self.driver.page_source
                keyword = check
                if keyword in Str:
                    last_time = datetime.now()               #获取启动后用时
                    break
            t = last_time - start_time          #得到总用时
            rt = str(t).split(":")               # 通过split函数对总用时进行切割,用来提取秒和毫秒,产生一个列表rt
            a = int(rt[1]) * 60                         # 提取分钟
            r = float(rt[2])                            # 提取到秒和毫秒
            run_time = a + r
            Total_time.append(run_time)
            self.driver.keyevent(3)
        print(Total_time)
        
if __name__ == "__main__":
    wb = bv_start()
    wb.platform('9', 'com.google.android.apps.messaging', 'com.google.android.apps.messaging.ui.ConversationListActivity')
    wb.execute(5, '微博', '首页')    	#测试5次求平均值
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章