Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to test page load time in selenium?

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.

like image 804
cookM Avatar asked Jul 06 '12 10:07

cookM


1 Answers

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
     */ 
    
like image 93
AutomatedTester Avatar answered Sep 28 '22 02:09

AutomatedTester