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进行性能测试的一个功能。

  • 相关阅读:
    [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)
    Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)
    [hdu5113]Black And White2014北京赛区现场赛B题(搜索加剪枝)
    BestCoder Round #20 部分题解(A,B,C)(hdu5123,5124,5125)
    2014年亚洲区域赛北京赛区现场赛A,D,H,I,K题解(hdu5112,5115,5119,5220,5122)
    UVA 11754 Code Feat (枚举,中国剩余定理)
    Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解
    UVA 11426 GCD
    UVALive 4119 Always an integer (差分数列,模拟)
    UVA 10253 Series-Parallel Networks (树形dp)
  • 原文地址:https://www.cnblogs.com/seniortestingdev/p/3357372.html
Copyright © 2011-2022 走看看