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

  • 相关阅读:
    mysql_Navicat数据库破解
    SpringBoot+ Mybatis 搭建
    SSH框架搭建
    SSM 框架搭建
    android 网络_网络图片查看器
    android 网络_网络源码查看器
    android ListView_显示数据库数据
    android ListView_新闻案例
    android ListView的怪异现象
    android ListView_Tiger
  • 原文地址:https://www.cnblogs.com/seniortestingdev/p/3357372.html
Copyright © 2020-2023  润新知