zoukankan      html  css  js  c++  java
  • Selenium简单测试页面加载速度的性能(Page loading performance)

    利用selenium的可以执行javascript脚本的特性,我写了一个java版本的获得页面加载速度的代码,这样你就可以在进行功能测试的同时进行一个简单的测试页面的加载速度的性能测试。

    我现在的项目用途主要是在功能测试的同时获得各个测试页面的加载速度,看看哪些页面的加载速度比较慢,如果加载的时间太慢,我就专门针对这个页面使用YSlow工具去检查一下这个页面,然后给出这个页面加载比较慢的建议,提交给开发人员,让他们自己去看看如何解决这个问题?

    通过调用通用的浏览器的Performance.timing接口API进而获得页面的真实加载速度。这里需要注意的是,通过调用API window.performance.timing.loadEventEnd,我发现在IE浏览器上可能返回的是Double类型的值,但是在Chrome或者是Firefox上返回的却是Long类型的,所以下面的代码中我就进行了不同类型的转换。

    通过调试,下面的代码可以用在IE,Chrome,Firefox浏览器上。其他的浏览器没有测试。

        /**
             * get the current page loading time ,it will return seconds
             * @param driver
             * 
             * @see http://www.softwareishard.com/blog/firebug/support-for-performance-timing-in-firebug/
             * @see http://selenium.polteq.com/en/implement-web-timings/
             * @see http://www.html5rocks.com/en/tutorials/webperformance/basics/
             * @see http://www.theautomatedtester.co.uk/blog/2010/selenium-webtimings-api.html
             */
            public long getPageLoadTime(){
                long pageloadtime=0;
                long pagestarttime=0;
                long pageendtime=0;
                
                //try{
                //different with browser ,ie will return is double value but firefox and chrome will return is long
                  Object startobject=executeJSReturn("return window.performance.timing.navigationStart;");
                Object endobject=executeJSReturn("return window.performance.timing.loadEventEnd;");
                //@SuppressWarnings("unchecked")
                // pagetimer=executeJSReturn("var performance = window.performance || window.webkitPerformance || window.mozPerformance || window.msPerformance || {};"+
                  //             " var timings = performance.timing || {};"+
                    //           " return timings;");
                //long pageloadend=(pagetimer.get("loadEventEnd"))/1000;
               //    long pageloadstart=(pagetimer.get("navigationStart"))/1000;
                //pageloadtime=(pageloadend-pageloadstart);
                //think it's the firefox or chrome browser
                if(startobject instanceof Long){
                    pagestarttime=(Long) startobject;
                    logger.debug("the page navigate start time is:"+pagestarttime);
                }
                if(startobject instanceof Double){
                    Double tempvalue=(Double) startobject;
                    pagestarttime=new Double(tempvalue).longValue();
                    logger.debug("the page navigate start time is:"+pagestarttime);
                }
                if(endobject instanceof Long){
                    pageendtime=((Long) endobject);
                    logger.debug("the page end time is:"+pageendtime);
                }
                if(endobject instanceof Double){
                    double tempvalue=(Double) endobject;
                    pageendtime=new Double(tempvalue).longValue();
                    logger.debug("the page end time is:"+pageendtime);
                }
                
                pageloadtime=(pageendtime-pagestarttime)/1000;
                logger.info("Get current page loading time is:"+pageloadtime);
            
                return pageloadtime;
            }
    View Code

    希望以上的代码可以帮助你进一步了解Selenium进行性能测试的一个功能。

  • 相关阅读:
    生活中头疼脑热及医生诊断用药相关,持续更新
    python3 面试题 英文单词全部都是以首字母大写,逐个反转每个单词
    python 代码如何打包成.exe文件(Pyinstaller)
    charles使用
    经典bug
    python3面试-查找字符串数组中的最长公共前缀
    python3面试题 按规律写出下一个数1,11,21,1211,111221
    python3 测试的时候如何批量随机生成伪数据?(faker模块的)
    python3面试题-一个包含n个整数的数组a,判断a中是否存在三个元素,a,b,c,使得a+b+c=0
    python3面试-将N(N<10000)个人排成一排,从第1个人开始报数;如果报数是M的倍数就出列
  • 原文地址:https://www.cnblogs.com/alterhu/p/3357372.html
Copyright © 2011-2022 走看看