在selenium中测试页面加载时间的正确方法? [英] Right way to test page load time in selenium?

查看:374
本文介绍了在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);

问题有时我会在页面真正加载之前得到时间结果(即我得到50ms次)所以我想在 driver.get()完成之前,控制权已交给下一条指令。

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?

编辑:

As user1258245建议我可以等一个元素加载,但问题是我不知道哪些页面预先加载了。

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.

推荐答案

有两种方法可以为您提供有意义的数据。

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


  1. 将浏览器代理与Selenium一起使用。这是python中的一个例子,但它与Java几乎相同

  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()


这是一个HAR文件从 proxy.har 返回,它只是一个JSON blob,它将为您提供所需的信息。我今年早些时候有关于它的博客

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. 另一种方法是使用导航时间规范在现代浏览器中可用。您需要做的就是执行一些javaScript,您将获得页面加载等的详细信息。

  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
 */ 


这篇关于在selenium中测试页面加载时间的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆