SelenIum + python 页面加载速度测试

  编写不易,求关注!!!
在这里插入图片描述

前言

  测试过程中,有人反映,页面加载速度慢,但是开发那边说:“不慢啊,我这里不到一秒就加载出来了”。
  其实浏览器有一个功能叫做缓存,缓存就是为了提高页面加载速度的。如果页面把缓存清除了在加载就能测试出页面加载速度。

  我的标题写的是用Selenium 来测试出结果,那也就是说是通过自动化来完成,那么怎么代码怎么写呢???
废话不多说了,直男直接看代码!!
  我的思路,是使用js获取页面的加载时间,然后使用execute_script方法执行js脚本,说明一点啊,js直接return的结果就是 execute_script 的返回值。不过,js是类型的,python也是弱类型的拿到返回值后腰给他一个数据类型,不然后期都没法处理。

获取页面加载时间

  下面代码里面有窗口切换,原因是页面加载一次后会有缓存存在,但是selenium打开浏览器打开新的页面的时候回自动清理缓存。

    # window.performance.timing; 对象参考地址:https://blog.csdn.net/lovenjoe/article/details/80260658

	    self.domready = """          // domready时间 
                    let mytiming = window.performance.timing;
                    return mytiming.domContentLoadedEventEnd   - mytiming.fetchStart ;
        """
        self.loadEventTime = """
                   let mytiming = window.performance.timing;
                   return mytiming.loadEventEnd - mytiming.navigationStart ;
                   """
    def __get_page_load_time_NoCache(self, Url, number=5):
        """
        网页无缓存的情况下进行加载速度测试
        :param Url: 加载的网址
        :param number: 加载次数
        :return:
        """
        driver = self.driver
        page = []
        domready = []
        res_page = {}
        res_domready = {}
        for i in range(number):
            # 调用浏览器打开一个新窗口
            driver.execute_script("window.open('','_blank');")
            # 窗口定位到新打开的窗口
            driver.switch_to.window(driver.window_handles[-1])
            driver.get(Url)
            page.append(int(driver.execute_script(self.loadEventTime)))
            domready.append(int(driver.execute_script(self.domready)))
            time.sleep(0.5)
            # 关闭窗口
            driver.execute_script("window.close();")
            # 窗口定位返回旧窗口
            driver.switch_to.window(driver.window_handles[-1])
		
		# 处理结果,可根据需要自己定义。
        res_page['max'] = max(page)
        res_page['min'] = min(page)
        res_page['avg'] = sum(page) / len(page)

        res_domready['max'] = max(domready)
        res_domready['min'] = min(domready)
        res_domready['avg'] = sum(domready) / len(domready)

        return {"页面加载时间": res_page,"DOM加载时间": res_domready}

  我先说明一点 这个脚本获取的数据和浏览器的network获取的数据是一样的!虽然没有接口,js、css、img那么详细!但是数据能保持一致!看下图截图!

  上面程序中 “ {“页面加载时间”: res_page,“DOM加载时间”: res_domready}” 返回的结果就是下图中我圈起来的部分!数据保持一致。
SelenIum + python 页面加载速度测试_第1张图片

最后奉上完整代码

# -*- coding: utf-8 -*-
import unittest
from selenium import webdriver
from object.DRIVER import getDriver
import time




class UntitledTestCase(unittest.TestCase):
    def setUp(self):
        Options = webdriver.ChromeOptions()
        Options.add_argument('--headless')
        # self.driver = webdriver.Chrome(chrome_options=Options)
        # self.driver = webdriver.Chrome()
        self.driver = getDriver()
        self.driver.implicitly_wait(30)
        self.verificationErrors = []
        self.accept_next_alert = True
        self.img = """           // 首屏图片加载完成 
                            let mytiming = window.performance.timing;
                            return window.lastImgLoadTime - mytiming.navigationStart ;
                """
        self.intfaces = """   https://blog.csdn.net/weixin_42284354/article/details/80416157
        // 接口完成加载完成 
                            let mytiming = window.performance.timing;
                            return Report.SPEED.LASTCGI - mytiming.navigationStart ;
                """
        self.DNS = """          // DNS查询耗时 
                    let mytiming = window.performance.timing;
                    return mytiming.domainLookupEnd - mytiming.domainLookupStart ;
        """
        self.TCP = """          // TCP链接耗时 
                    let mytiming = window.performance.timing;
                    return mytiming.connectEnd - mytiming.connectStart ;
        """
        self.request = """          // request请求耗时 
                    let mytiming = window.performance.timing;
                    return mytiming.responseEnd  - mytiming.responseStart ;
        """
        self.dom = """          //  解析dom树耗时 
                    let mytiming = window.performance.timing;
                    return mytiming.domComplete - mytiming.domInteractive ;
        """
        self.Ari = """          // 白屏时间 
                    let mytiming = window.performance.timing;
                    return mytiming.responseStart - mytiming.navigationStart ;
        """

        self.domready = """          // domready时间 
                    let mytiming = window.performance.timing;
                    return mytiming.domContentLoadedEventEnd   - mytiming.fetchStart ;
        """
        self.loadEventTime = """
                   let mytiming = window.performance.timing;
                   return mytiming.loadEventEnd - mytiming.navigationStart ;
                      """
        self.gather_data_dict = [
            {'UrlName': 'baidu',
             'Url': 'https://www.baidu.com/',
             'number': 20}
        ]
        # 添加压测网址,在数组汇总添加一个 Dict即可
        self.gather_data_dict_ = [
            {'UrlName': '首页',
             'Url': 'http://zs.cnknowledge.com/',
             'number': 20},
            {'UrlName': '政策法规',
             'Url': 'http://zs.cnknowledge.com/html/policy_Law/law_know.html?parentId=3&type=1',
             'number': 20},
            {'UrlName': '行业新闻',
             'Url': 'http://zs.cnknowledge.com/html/trade_News/news_pages.html?parentId=1&type=2',
             'number': 20},
            {'UrlName': '学术前言',
             'Url': 'http://zs.cnknowledge.com/html/study_Leading/study_news.html?parentId=2&type=3',
             'number': 20},
            {'UrlName': '专题报道',
             'Url': 'http://zs.cnknowledge.com/html/special_report/report_event.html?parentId=4&type=4',
             'number': 20},
            {'UrlName': '名录',
             'Url': 'http://zs.cnknowledge.com/html/list_new/organization.html?parentId=5&type=5',
             'number': 20}
        ]

    def test_untitled_test_case(self):
        # 返回结果
        result = []
        # 读取压测数数据,返回加载结果!
        for data in self.gather_data_dict:
            result_temp = {
                "UrlName": data["UrlName"],
                "Url": data["Url"],
                "number": data["number"],
                "NoCache": self.__get_page_load_time_NoCache(data['Url'], data['number']),
                "Cache": self.__get_page_load_time_Cache(data['Url'], data['number'])
            }
            result.append(result_temp)
        print(result)

    def __get_page_load_time_NoCache(self, Url, number=1):
        """
        网页无缓存的情况下进行加载速度测试
        :param Url: 加载的网址
        :param number: 加载次数
        :return:
        """
        driver = self.driver
        page = []
        domready = []
        res_page = {}
        res_domready = {}
        for i in range(number):
            # 调用浏览器打开一个新窗口
            driver.execute_script("window.open('','_blank');")
            # 窗口定位到新打开的窗口
            driver.switch_to.window(driver.window_handles[-1])
            driver.get(Url)
            page.append(int(driver.execute_script(self.loadEventTime)))
            domready.append(int(driver.execute_script(self.domready)))
            time.sleep(0.5)
            # 关闭窗口
            driver.execute_script("window.close();")
            # 窗口定位返回旧窗口
            driver.switch_to.window(driver.window_handles[-1])

        res_page['max'] = max(page)
        res_page['min'] = min(page)
        res_page['avg'] = sum(page) / len(page)
        print(res_page)
        res_domready['max'] = max(domready)
        res_domready['min'] = min(domready)
        res_domready['avg'] = sum(domready) / len(domready)
        
        return {"页面加载时间": res_page,
                "DOM加载时间": res_domready}

    def __get_page_load_time_Cache(self, Url, number=5):
        """
        网页有缓存的情况下进行加载速度测试
        :param Url: 加载的网址
        :param number: 加载次数
        :return:
        """
        driver = self.driver
        page = []
        domready = []
        res_page = {}
        res_domready = {}
        driver.get(Url)
        for i in range(number):
            driver.get(Url)
            page.append(int(driver.execute_script(self.loadEventTime)))
            domready.append(int(driver.execute_script(self.domready)))

        res_page['max'] = max(page)
        res_page['min'] = min(page)
        res_page['avg'] = sum(page) / len(page)

        res_domready['max'] = max(domready)
        res_domready['min'] = min(domready)
        res_domready['avg'] = sum(domready) / len(domready)

        return {"页面加载时间": res_page,
                "DOM加载时间": res_domready}

    def tearDown(self):
        pass


  下面是处理的代码,我没怎么算,就是打印出来写了一个报告。

# -*- coding: utf-8 -*-
result = [{'UrlName': 'baidu', 'Url': 'https://www.baidu.com/', 'number': 3,
           'NoCache': {'页面加载时间': {'max': 523, 'min': 284, 'avg': 400.6666666666667},
                       'DOM加载时间': {'max': 439, 'min': 256, 'avg': 348.6666666666667}},
           'Cache': {'页面加载时间': {'max': 131, 'min': 129, 'avg': 130.0},
                     'DOM加载时间': {'max': 101, 'min': 85, 'avg': 94.0}}}]

for re in result:
    print(re["UrlName"])
    print(re["Url"])
    Cache_page = re["Cache"]['页面加载时间']
    Cache_dom = re["Cache"]['DOM加载时间']
    NoCache_page = re["NoCache"]['页面加载时间']
    NoCache_dom = re["NoCache"]['DOM加载时间']
    print(Cache_page["max"], Cache_page["min"], Cache_page["avg"], Cache_dom["max"], Cache_dom["min"], Cache_dom["avg"])
    print(NoCache_page["max"], NoCache_page["min"], NoCache_page["avg"], NoCache_dom["max"], NoCache_dom["min"],
          NoCache_dom["avg"])

  看不清就看不清吧,就是一个意思!
SelenIum + python 页面加载速度测试_第2张图片

在这里插入图片描述

你可能感兴趣的:(Selenium功能测试,python,语言)