Correct way to check page load time in selenium?

I am trying to program the load time of a list of websites. The goal is to roughly simulate the page load time that the user will perceive.

My first approach is to call inside a loop:

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

The problem is that sometimes I get the result of the time before the page actually loads (for example, I get 50 ms times), so I think that control is passed to the next command until driver.get() .

What should I do to improve this test?

EDIT:

As user1258245 suggested I wait for the item to load, but the problem is that I don’t know which pages will load in advance.

+8
java selenium delay web-testing
source share
1 answer

There are two ways to do this, which will give you meaningful data.

  • Use a Browsermob proxy server with selenium. This is an example in python, but it is almost the same as 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 block, will provide you with the information you need. I blogged about it earlier this year.

  1. Another approach is to use the timing specifications of navigation available in modern browsers. All you have to do is execute some javaScript and you will get detailed information about page loading, 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 */ 
+13
source share

All Articles