Right way to test page load time in selenium?

﹥>﹥吖頭↗ 提交于 2019-11-30 12:16:28

问题


I'm trying to programatically test the load time of a list of websites. The purpose is to roughly simulate the page load time a user will perceive.

My first approach is to call the following inside a loop:

    startTime = System.currentTimeMillis();
    driver.get("http://" + url);
    diff = System.currentTimeMillis() - startTime;
    System.out.println("Load time was " + diff);

The problem is sometimes I get the time result before the page has really loaded (i.e i get 50ms times) so I guess the control is being handed to the next instruction before the driver.get() has completed.

What should I do to improve this test?

EDIT:

As user1258245 suggested I could wait for an element to load but the problem is I don't know which pages ill be loading beforehand.


回答1:


There are 2 way to do this that will give you meaningful data.

  1. Use Browsermob Proxy with Selenium. This is an example in python but its pretty much the same in Java

    from browsermobproxy import Server
    server = Server("path/to/browsermob-proxy")
    server.start()
    proxy = server.create_proxy()
    
    from selenium import webdriver
    profile  = webdriver.FirefoxProfile()
    profile.set_proxy(proxy.selenium_proxy())
    driver = webdriver.Firefox(firefox_profile=profile)
    
    proxy.new_har("google")
    driver.get("http://www.google.co.uk")
    proxy.har # returns a HAR JSON blob
    
    proxy.stop()
    driver.quit()
    

The HAR file that is returned from proxy.har, which is just a JSON blob, will give you the information that you need. I blogged about it earlier this year

  1. The other approach is to use the navigations timings spec available in modern browsers. All that you need to do is execute some javaScript and you will get details of page load etc.

    ((JavascriptExecutor)driver).executeScript("var performance = window.performance || {};" + 
                "var timings = performance.timing || {};"+
                "return timings;");
    
    /* The hashmap returned will contain something like the following.
     * The values are in milliseconds since 1/1/1970
     *
     * connectEnd: 1280867925716
     * connectStart: 1280867925687
     * domainLookupEnd: 1280867925687
     * domainLookupStart: 1280867925687
     * fetchStart: 1280867925685
     * legacyNavigationStart: 1280867926028
     * loadEventEnd: 1280867926262
     * loadEventStart: 1280867926155
     * navigationStart: 1280867925685
     * redirectEnd: 0
     * redirectStart: 0
     * requestEnd: 1280867925716
     * requestStart: 1280867925716
     * responseEnd: 1280867925940
     * responseStart: 1280867925919
     * unloadEventEnd: 1280867925940
     */ 
    


来源:https://stackoverflow.com/questions/11360854/right-way-to-test-page-load-time-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!