java 在 selenium 中测试页面加载时间的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11360854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:46:08  来源:igfitidea点击:

Right way to test page load time in selenium?

javaseleniumdelayweb-testing

提问by cookM

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.

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

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.

正如 user1258245 建议的那样,我可以等待一个元素加载,但问题是我不知道事先加载了哪些页面。

回答by AutomatedTester

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()
    
  1. 将 Browsermob 代理与 Selenium 结合使用。这是python中的一个例子,但它在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 bloggedabout it earlier this year

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

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

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